简体   繁体   中英

Display Image using asset path or local file path

I'm trying to display a table of images from the camera roll using swift. I have the asset path:

assets-library://asset/asset.JPG?id=123456789&ext=JPG

and the local file path:

/Users/johndoe/Library/Developer/CoreSimulator/Devices/1932149090125/data/Containers/Data/Application/11451-54365436-34563-2345-232342364/Documents/asset.JPG

I don't know what function to use to create a UIImage.

I've tried UIImage(data: NSdata) , and UIImage(contentsOfFile: String) with no success.

Code : Inside my UITableView, cellForRowAtIndexPath function, I'm looping through a database that has the cell's asset path and local file path.

let url = NSURL(string: "/Users/johndoe/Library/Developer/CoreSimulator/Devices/123455678/data/Containers/Data/Application/124536432342364/Documents/asset.JPG")
    let data = NSData(contentsOfURL: url!)
    cell.thumbnailImage.image = UIImage(data: data!)

I'm trying to display a table of images from the camera roll using swift

Well, it's an elaborate business. You have to use PhotoKit. Obtain the photo as a PHAsset, and call PHImageManager.defaultManager().requestImageForAsset to get the actual image. The image arrives asynchronously and can arrive multiple times as the quality of the image improves, so you have to be prepared for that.

There's a function, which is called when a picture has been selected in the imagePickerController, see below. Make sure your ViewController uses the UIImagePickerControllerDelegate protocol.

Then in my .m-file I have the function belonging to that protocol:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { // Comes here after each picture, photographed or picked

UIImage *img = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

etc, etc }

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