UIImagePickerController + Cropping:allowEditing是什么,它不是什么?

时间:2014-06-10 19:48:22

标签: ios objective-c image ios7 uiimagepickercontroller

在阅读了有关UIImagePickerController的文档和sample code from apple之后,在Stackoverflow(Questions)阅读了许多教程甚至or here后,我得出结论,内置UIImagePicker能够呈现一个"裁剪窗口"到用户并让用户裁剪并移动他想要使用的图像的右侧部分。

但是在iOS 7.1上查看这些示例,似乎没有裁剪窗口。什么都没有让用户裁剪出拍摄图像的一部分。所有测试都发生在真实硬件(iPhone 5S)上,因为缺少相机而没有使用iPhone模拟器;)

此外,在委托方法" didFinishPickingMediaWithInfo"没有输入" UIImagePickerControllerEditedImage"在" info" -NSDictionary中。

我做了什么:

    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];

#if TARGET_IPHONE_SIMULATOR
    // iPhone-Simulator has no camera, just save images to the photoalbum on the simulator to use them here
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
#else
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
#endif

    imagePickerController.editing = YES;
    imagePickerController.delegate = (id)self;
    imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;


    [self presentModalViewController:imagePickerController animated:YES];

和委托方法:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        // .... do something 
}

此时,字典中只有3个键可用,名称为" info":

  • UIImagePickerControllerOriginalImage
  • UIImagePickerControllerMediaMetadata
  • UIImagePickerControllerMediaType
别无其他。此外,没有出现裁剪窗口或任何类似控件。

我对ImagePicker行为的期望是错误的吗?在此SF-Question中,用户编写了"Crop window comes up"。该功能被删除了吗?文件是否过时或我在这里完全错了?

我还尝试从另一个answer to a similar question获取此代码:

- (void)    imagePickerController:(UIImagePickerController *)picker 
    didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    [picker dismissViewControllerAnimated:YES completion:^{
          // Edited image works great (if you allowed editing)
        UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
          // AND the original image works great
        UIImage *originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
          // AND do whatever you want with it, (NSDictionary *)info is fine now
        UIImage *myImage = [info objectForKey:UIImagePickerControllerEditedImage];
    }];
}

没有出现裁剪窗口,键" UIImagePickerControllerEditedImage"不存在于info-NSDictionary。

有人可以解释我正确的行为吗?非常感谢你提前。

2 个答案:

答案 0 :(得分:3)

这个对我有用:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *originalImage, *editedImage;

    editedImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];
    originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];

    if (editedImage)
        self.profilePictureImageView.image = editedImage;
    else
        self.profilePictureImageView.image = originalImage;

    [picker dismissViewControllerAnimated:YES completion:nil];
}

答案 1 :(得分:0)

首先确保导入以下头文件(必须将MobileCoreServices框架导入项目):

#import <MobileCoreServices/UTCoreTypes.h>

然后尝试以下代码

    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];

#if TARGET_IPHONE_SIMULATOR
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum]){
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    }
#else
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]){
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }
#endif

    imagePicker.mediaTypes = @[(NSString *)kUTTypeImage];
    imagePicker.allowsEditing = YES;
    imagePicker.delegate = self;

    //[self presentModalViewController:imagePicker animated:YES]; <-- This method is deprecated use the following instead

    [self presentViewController:imagePicker animated:YES completion:nil];