简体   繁体   中英

How to fetch an image from photo library of iOS using URL path?

I used UIImagePickerController to select an image from photo library of an iOS device. Then I saved the URL path of the selected image in database using Core Data. Now I want to use that saved URL path to fetch the image in another view controller. How can I fetch the image using URL path using Objective-C? Is there any framework for that? I found ALAssets framework is deprecated.

You should not save the URL for the image, unless you saved it into a more permanent location on the device. UIImagePickerController will provide you a URL to a temporary image, but this may be deleted after you close the app.

Instead, grab the actual UIImage locally and save that as a blob with CoreData.

let image = info[UIImagePickerControllerOriginalImage] as? UIImage
let imageData = UIImageJPEGRepresentation(image, 1.0)

From here, save the data as a blob to a CoreData database. This is an article in itself, so I won't go into depth here. Refer to a resource such as http://pinkstone.co.uk/how-to-save-a-uiimage-in-core-data-and-retrieve-it/

To retrieve that image from the data, fetch the blob from CoreData and convert it to a UIImage:

let imageData = < Core Data Fetch and Processing >
if let image = UIImage(data: imageData) {
    // Use your image
}

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