EvanS
EvanS

Reputation: 21

How to display photo library image in Xcode?

My objective-c code is erroring out with the Code=13 error, and does not display either the cameras photo or a library's image in an image view.

I'M Using Xcode 10.1, deployment target 12.0, and I'm going off example from UIImagePickerController not picking image in iOS 9

I have two problems:

1) Even though I add in "Privacy - Photo Library Usage Description | We want to use the library" in the p-list, it still gives me this error:

[discovery] errors encountered while discovering extensions: Error Domain=PlugInKit Code=13 "query cancelled" UserInfo={NSLocalizedDescription=query cancelled}

What am I doing wrong?

2) I can't get the image to show up in the LoveImage view. What am I doing wrong here?

Below is .m file:

-(IBAction)imagepickertapped:(id)sender { 
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Attach image" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction* pickFromGallery = [UIAlertAction actionWithTitle:@"Take a photo"
                                                          style:UIAlertActionStyleDefault
                                                        handler:^(UIAlertAction * action) {
                                                            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
                                                            {
                                                                UIImagePickerController* picker = [[UIImagePickerController alloc] init];
                                                                picker.sourceType = UIImagePickerControllerSourceTypeCamera;
                                                                picker.delegate = self;
                                                                [self presentViewController:picker animated:YES completion:NULL];
                                                            }

                                                        }];
UIAlertAction* takeAPicture = [UIAlertAction actionWithTitle:@"Choose from gallery"
style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * action) {
                                                         UIImagePickerController* picker = [[UIImagePickerController alloc] init];
                                                         picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                                                         picker.delegate = self;
                                                         [self presentViewController:picker animated:YES completion:NULL];
                                                     }];
UIAlertAction* cancel = [UIAlertAction actionWithTitle:@"Cancel"
                                         style:UIAlertActionStyleCancel
                                               handler:^(UIAlertAction * action) {
                                               }];

[alertController addAction:pickFromGallery];
[alertController addAction:takeAPicture];
[alertController addAction:cancel];
[self presentViewController:alertController animated:YES completion:nil]; }

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
UIImage *LoveImage = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
self.LoveImage = info[UIImagePickerControllerOriginalImage];}

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

Upvotes: 2

Views: 778

Answers (1)

Sagar koyani
Sagar koyani

Reputation: 431

Change your didfinish method code to below.

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    UIImage *LoveImage = info[UIImagePickerControllerOriginalImage];
    [picker dismissViewControllerAnimated:YES completion:nil];
    self.LoveImage.image = LoveImage;
}

LoveImage is an image view. We can't assign an image to it directly. It has an image property so we have to set its image property.

Upvotes: 1

Related Questions