简单的使用ios自带拍照裁剪功能

今天项目里要用到图片的裁剪功能,本来想找第三方,但是查了下资料,觉得如果只是要简单的实现其实也不难,所以决定自己写一下

// 关于图片的选择
 //点击要选取图片的view的时候要弹出两个选项,拍照和选取本地
- (void)addImageClick
{
    [_drawlineView removeFromSuperview];
    //    [self takePhotoAction];
    
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"请选择获取方式" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
        // Cancel button tappped.
        [self dismissViewControllerAnimated:YES completion:^{
        }];
    }]];
    
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        // 拍摄新照片
        [self takePhotoAction];
        
    }]];
    
    [actionSheet addAction:[UIAlertAction actionWithTitle:@"从相册选" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        // 从相片中读取
        [self browseAlbum];
        
    }]];
    
    [self presentViewController:actionSheet animated:YES completion:nil];
    
}
//拍照
//最好就是要判断一下相机可不可用, allowsEditing是用来设置照片是否可以编辑,就是会不会出现一个框框来约束图片的比例
// sourceType  在拍照时用的 UIImagePickerControllerSourceTypeCamera  在选取图片时用 UIImagePickerControllerSourceTypeSavedPhotosAlbum
- (void)takePhotoAction {
    BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
    if (!isCamera) { //若不可用,弹出警告框
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"无可用摄像头" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alert show];
        return;
    }
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.allowsEditing = YES;
    imagePicker.delegate = self;
    [self presentViewController:imagePicker animated:YES completion:nil];
}
//不管拍照还是选取本地图片,完成后都会调用这个方法
//info 里面放的图片的所有信息  UIImagePickerControllerOriginalImage这个key拿的是原图   UIImagePickerControllerEditedImage拿的是图片裁剪后的图
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    NSString *mediaType = info[@"UIImagePickerControllerMediaType"];
    if ([mediaType isEqualToString:@"public.image"]) {  //判断是否为图片

         UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];//这里拿到图片

         //拿到图片,你要做什么事........
         {
           //do something
         }


         //通过判断picker的sourceType,如果是拍照则保存到相册去
        if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
            UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
            
          
        }

    }
 }

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error
  contextInfo:(void *)contextInfo
{
    // Was there an error?
    if (error != NULL)
    {
        // Show error message...
        [WSToast showToastWithTitle:error.description duration:TOAST_VIEW_TIME completeCallBack:nil];
    }
    else  // No errors
    {
        // Show message image successfully saved
        [WSToast showToastWithTitle:@"图片已保存" duration:TOAST_VIEW_TIME completeCallBack:nil];
    }
}

  WSToast 是我项目里面封装的提示展示框,当然,这只是简单的实现裁剪,裁剪的比例是固定不变的,而且也不能多选,就是简单的需求而已

原文地址:https://www.cnblogs.com/yulongjiayuan/p/5857809.html