简体   繁体   中英

UIImagePickerController and photo library access

I had just made a post about images not being displayed but the question came out ambigous and I lacked information on the post, so I'd like to start over with a more clear concise question.

I have an app in which I allow a user to select an image using UIImagePickerController. In my viewDidLoad I do this

_pickercontroller = [[UIImagePickerController alloc] init];
_pickercontroller.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;

I then present the controller allowing the user to select an image

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

Then in my didFinishPickingMediaWithInfo I save the image name so I can display it later when the app starts up again

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info{
    _selectedimage = info[UIImagePickerControllerOriginalImage];
    _selectedimageurl = info[UIImagePickerControllerImageURL];


    [picker dismissViewControllerAnimated:YES completion:nil];
    //[self.image setImage:target];

    //store this string later to be used
    NSString* path = [_selectedimageurl relativeString];
}

From there, I have a UIImageView which displays the image the user selected by creating a URL from that relativeString and an image object like so

NSURL* url = [[NSURL alloc] initWithString:path];
[image setImage:[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:url]]];

And this works fine... BUT! When I save NSString* path = [_selectedimageurl relativeString]; to a file or database and then I close and reopen the app, I try to display the image again and it no longer gets displayed. It shows up empty. And I found out why this was happening,

It has to do with the imagepickercontroller, for some reason calling [self presentViewController:_pickercontroller animated:YES completion:nil]; allows access to the photo library for the duration that the app is running, so reading an image from the saved image name and displaying it works. But if [self presentViewController:_pickercontroller animated:YES completion:nil]; has not been called, then trying to load the image from the imagename will display nothing.

So I'm just wondering how do I get around this? Because I don't want to launch the imagepickercontroller if an image is already saved. I just want the saved image to be displayed. But I can't display the image unless I call [self presentViewController:_pickercontroller animated:YES completion:nil]; ...

Should I not use the imagepickercontroller? Should I use something else? Should I save the image name differently? Has anyone dealt with this?

Thanks

Edit: First solution allow access to the photo library with Privacy - Photo Library Usage Description I have done this and I can access the files after terminating and reopening the app, but then I face another problem. If the app is deleted and redownloaded, that path is no longer valid to the image

Since it says to avoid answering questions in comments, I'm putting this here. So, you actually are getting the file path of an image on the user's device and trying to load it at a later time. I don't think this is allowed. Unless you ask permission for it, which is what is happening when you use UIImagePickerController . What you need to do is to store the images using FileManager API to write to a directory store the URL paths and then load them whenever required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM