简体   繁体   English

UIImagePickerController 和照片库访问

[英]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.我有一个应用程序,我允许用户使用 UIImagePickerController select 图像。 In my viewDidLoad I do this在我的 viewDidLoad 我这样做

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

I then present the controller allowing the user to select an image然后我展示 controller 允许用户 select 图像

[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然后在我的didFinishPickingMediaWithInfo中保存图像名称,以便稍后在应用程序再次启动时显示它

-(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从那里,我有一个 UIImageView 显示用户通过从该 relativeString 和图像 object 创建一个 URL 选择的图像,像这样

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];当我保存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];它与 imagepickercontroller 有关,出于某种原因调用[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];但是如果[self presentViewController:_pickercontroller animated:YES completion:nil]; has not been called, then trying to load the image from the imagename will display nothing.尚未调用,然后尝试从 imagename 加载图像将不显示任何内容。

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.因为如果图像已经保存,我不想启动 imagepickercontroller。 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];但我无法显示图像,除非我调用[self presentViewController:_pickercontroller animated:YES completion:nil]; ... ...

Should I not use the imagepickercontroller?我不应该使用 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.编辑:第一个解决方案允许访问带有Privacy - Photo Library Usage Description我已经这样做了,我可以在终止并重新打开应用程序后访问这些文件,但是我遇到了另一个问题。 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 .除非您请求许可,否则当您使用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.您需要做的是使用FileManager API 存储图像以写入存储 URL 路径的目录,然后在需要时加载它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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