美文网首页
相机技术图片压缩技术

相机技术图片压缩技术

作者: WhisperMax | 来源:发表于2016-03-09 20:20 被阅读0次

    调用系统图片库以及摄像头

    判断当前设备是否有摄像头

    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
    

    创建UIAlertController选择

    - (void)openLocalPhoto
    {
         UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"获取图片" message:@"请选择方式" preferredStyle:UIAlertControllerStyleActionSheet];
        
         UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                UIImagePickerController *imagePikerController = [[UIImagePickerController alloc]init];
                imagePikerController.delegate = self;
                imagePikerController.allowsEditing = YES;
                imagePikerController.sourceType = UIImagePickerControllerSourceTypeCamera;
                MYLog(@"相机");
                [self presentViewController:imagePikerController animated:YES completion:nil];
            }];
    
        UIAlertAction *defaultAction1 = [UIAlertAction actionWithTitle:@"从相册获取" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
            imagePickerController.delegate = self;
            imagePickerController.allowsEditing = YES;
            imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            MYLog(@"相册");
            [self presentViewController:imagePickerController animated:YES completion:nil];
        }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            MYLog(@"取消");
        }];
        
        [alertController addAction:defaultAction];
        [alertController addAction:cancelAction];
        [alertController addAction:defaultAction1];
        
        [self presentViewController:alertController animated:YES completion:nil];
        
    }```
    
    ###以下是使用相机技术需要遵守的两个协议方法
    
    _<UIImagePickerControllerDelegate,UINavigationControllerDelegate>_
    
    点击完成后怎么做
    
    • (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
      {
      UIImage *image = info[UIImagePickerControllerEditedImage];
      self.headerImageView.image = image;
      [self dismissViewControllerAnimated:picker completion:nil];
      }```
      点击取消按钮怎么做
    -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
        [picker dismissViewControllerAnimated:YES completion:nil];
    }```
    
    ###iOS 8.0以前使用的方法
    遵守四个协议
    
    _<UIImagePickerControllerDelegate,UINavigationControllerDelegate,UIActionSheetDelegate,UIAlertViewDelegate>_
    

    -(void)open{
    UIActionSheet *sht = [[UIActionSheet alloc]initWithTitle:@"请选择"
    delegate:self
    cancelButtonTitle:@"取消"
    destructiveButtonTitle:@"相机"
    otherButtonTitles:@"相册", nil];
    }```

    -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
        if (buttonIndex == 2) {
            MYLog(@"取消");
        }else if (buttonIndex == 1){
            MYLog(@"相册");
            UIImagePickerController *imagePicker = [UIImagePickerController new];
            imagePicker.delegate = self;
            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            imagePicker.allowsEditing = YES;
            [self presentViewController:imagePicker animated:YES completion:nil];
        }else{
            MYLog(@"相机");
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                UIImagePickerController *imagePicker = [UIImagePickerController new];
                imagePicker.delegate = self;
                imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                imagePicker.allowsEditing = YES;
                [self presentViewController:imagePicker animated:YES completion:nil];
            }else{
                MYLog(@"无法识别到相机");
            }
        }
    }```
    
    ##压缩照片技术
    
    • (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString ,id> )info
      {
      UIImage image = info[UIImagePickerControllerOriginalImage];
      MYLog(@"image length = %ld",UIImagePNGRepresentation(image).length);//原图
      /
      缩略图
      /
      UIImage *newImage = [self thumnailWithImage:image size:CGSizeMake(100, 100)];
      MYLog(@"newImage length = %ld",UIImagePNGRepresentation(newImage).length);

      /** 发送数据 二次压缩图*/
      NSData *sendData = UIImageJPEGRepresentation(newImage, 0.05);//参数越高 质量越好0-1
      MYLog(@"sendData length = %ld",sendData.length);
      [self realSendImageMsg:sendData];

      [self dismissViewControllerAnimated:picker completion:nil];
      }```
      ** 生成缩略图**

    -(UIImage *)thumnailWithImage:(UIImage *)image size:(CGSize)size{
        UIImage *newImage = nil;
        if (image != nil) {
            UIGraphicsBeginImageContext(size);
            
            [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
            newImage = UIGraphicsGetImageFromCurrentImageContext();
    
            UIGraphicsEndImageContext();
        }
        return newImage;
    }```
    ** 发送图片消息**
    

    -(void)realSendImageMsg:(NSData *)data{
    //把data 转成文本 base64编码
    NSString *base64Str = [data base64EncodedStringWithOptions:0];
    //构建图片消息对象
    XMPPMessage *msg = [XMPPMessage messageWithType:@"chat" to:self.friendJid];
    [msg addBody:base64Str];
    //发送消息
    [[KRXmppTool sharedKRXmppTool].xmppStream sendElement:msg];
    }```

    -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
        [picker dismissViewControllerAnimated:YES completion:nil];
    }
    

    相关文章

      网友评论

          本文标题:相机技术图片压缩技术

          本文链接:https://www.haomeiwen.com/subject/jfctlttx.html