简体   繁体   English

在没有选择器的情况下从照片库加载图像?

[英]Load image from photo library without a picker?

Here is what I need. 这就是我所需要的。

I need my users to be able to pick an image from the photo library, however, they should not need to have to repick it each time they use it. 我需要我的用户能够从照片库中选择图像,但是,他们不必每次使用时都必须重新选择它。 Instead, I would like to remember the photo's path to be able to load it in the future. 相反,我想记住照片的路径,以便将来可以加载它。 Is this possible? 这可能吗? If not, what are the alternatives? 如果没有,有哪些替代方案? Is it possible to cache the photo to a local folder or place? 是否可以将照片缓存到本地文件夹或位置?

Thanks 谢谢

You can't keep an reference to the picked image, so you need to, as you suggest your self, save a local copy of the image. 您无法保留对所选择图像的引用,因此,如您所愿,您需要保存图像的本地副本。 You could use the UIImagePicker to let the user pick the image and then save it locally: 您可以使用UIImagePicker让用户选择图像,然后将其保存在本地:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
   UIImage* image = (UIImage*) [info objectForKey:UIImagePickerControllerOriginalImage];
   NSData *imageData = UIImagePNGRepresentation(image);

 //save the imageData to document dir. You could also save it to the cache...
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documents = [paths objectAtIndex:0];
   NSString *finalPath = [documents stringByAppendingPathComponent:@"myImageName.png"];
   [imageData writeToFile:finalPath atomically:YES];
}

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

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