Domanda

I am trying to simply select an image from the iphone’s library and display it on the view. Here is the header file reference:

@interface myappViewController : UIViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate>{
UIImagePickerController *imagepicker;
UIImagePickerController *imagepicker2;
UIImage *image;
IBOutlet UIImageView *imagepickerview;

}

- (IBAction)TakePhoto;
- (IBAction)ChooseExisting;

@end

Here is the implementation code:

@interface myappViewController ()

@end

@implementation myappViewController

- (IBAction)TakePhoto{

imagepicker = [[UIImagePickerController alloc] init];
imagepicker.delegate = self;
[imagepicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:imagepicker animated:YES completion:NULL];

}

- (IBAction)ChooseExisting{

imagepicker2 = [[UIImagePickerController alloc] init];
imagepicker2.delegate = self;
[imagepicker2 setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:imagepicker2 animated:YES completion:NULL];

}

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

image = [info objectForKey:UIImagePickerControllerOriginalImage];
[imagepickerview setImage:image];

}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)imagepickerview{

[self dismissViewControllerAnimated:YES completion:NULL];

}

On the storyboard I have two buttons. One for choose existing and one for take photo. If I select the choose existing the library opens and I can select an image. The problem is that the image is not selected and then redirected back to the xcode view. Rather, I have to click the cancel button in the navigation controller of the photo library and then I see the image loaded in the view. Any idea why this is not properly loading?

È stato utile?

Soluzione

Please use valueForKey instead of objectForKey also dismiss the image picker before getting the image.

[self dismissViewControllerAnimated:NO completion:nil];

image = [info valueForKey:UIImagePickerControllerOriginalImage];
[imagepickerview setImage:image];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top