Getting image from UIImagePickerController

2020-06-16 03:29发布

问题:

I want to get the image frm imagepickercontroller.But my code doesn't have any effect Here is the code..

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];
    imageview.image = [UIImage imageNamed:[info objectForKey:@"UIImagePickerControllerOriginalImage"]];

回答1:

You can try this code to access an Image returned from the picker

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    [picker dismissModalViewControllerAnimated:YES];

    UIImage* originalImage = nil;
    originalImage = [info objectForKey:UIImagePickerControllerEditedImage];
    if(originalImage==nil)
    {
        originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    if(originalImage==nil)
    {
        originalImage = [info objectForKey:UIImagePickerControllerCropRect];
    }
  //At this point you have the selected image in originalImage
    [self saveImageToDocumentDirectory:originalImage];
}


回答2:

Try

imageview.image = (UIImage*) [info objectForKey:UIImagePickerControllerOriginalImage];


回答3:

Updated for Swift 5, the following will safely unwrap the UIImage.

// This uses the full key value
guard let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage else { return }

// This uses only the info key value for shorter code
guard let image = info[.originalImage] as? UIImage else { return }


回答4:

just try this

imageview.image = [info objectForKey:UIImagePickerControllerOriginalImage];