Peter
Peter

Reputation: 87

Get Thumbnail Image from PHAsset

I want to get the Thumbnail Image of my PHAsset. I already extracted a PHAsset from the Photo Library and want to get the Thumbnail Image now.

Can you help me in Objective-C?

Thanks!

Upvotes: 2

Views: 7826

Answers (4)

Francois Nadeau
Francois Nadeau

Reputation: 7463

In case someone is looking for a swift solution, here is an extension:

extension PHAsset {
    var thumbnailImage : UIImage {
        get {
            let manager = PHImageManager.default()
            let option = PHImageRequestOptions()
            var thumbnail = UIImage()
            option.isSynchronous = true
            manager.requestImage(for: self, targetSize: CGSize(width: 300, height: 300), contentMode: .aspectFit, options: option, resultHandler: {(result, info)->Void in
                thumbnail = result!
            })
            return thumbnail
        }
    }
}

Upvotes: 8

CodeBender
CodeBender

Reputation: 36610

Here is a complete answer for Swift 4 showing the function & call against it. Also, make sure you have the photos privacy flag set in your plist.

import Photos

func requestImage(for asset: PHAsset,
                  targetSize: CGSize,
                  contentMode: PHImageContentMode,
                  completionHandler: @escaping (UIImage?) -> ()) {
    let imageManager = PHImageManager()
    imageManager.requestImage(for: asset, 
                              targetSize: targetSize, 
                              contentMode: contentMode, 
                              options: nil) { (image, _) in
                                  completionHandler(image)
    }
}

let asset = // your existing PHAsset
let targetSize = CGSize(width: 100, height: 100)
let contentModel = PHImageContentMode.aspectFit
requestImage(for: asset, targetSize: targetSize, contentMode: contentModel, completionHandler: { image in
    // Do something with your image if it exists
})

Upvotes: 2

Sangram Shivankar
Sangram Shivankar

Reputation: 3573

  PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init]; 
    options.resizeMode = PHImageRequestOptionsResizeModeExact;

  NSInteger retinaMultiplier = [UIScreen mainScreen].scale;
  CGSize retinaSquare = CGSizeMake(imageView.bounds.size.width * retinaMultiplier, imageView.bounds.size.height * retinaMultiplier);

    [[PHImageManager defaultManager]
             requestImageForAsset:(PHAsset *)_asset
                       targetSize:retinaSquare
                      contentMode:PHImageContentModeAspectFill
                          options:options
                    resultHandler:^(UIImage *result, NSDictionary *info) {

                    imageView.image =[UIImage imageWithCGImage:result.CGImage scale:retinaMultiplier orientation:result.imageOrientation];

    }];

i get this answer from How to fetch squared thumbnails from PHImageManager?

Upvotes: 1

Abizern
Abizern

Reputation: 150605

The PHImageManagerClass has the method:

- requestImageForAsset:targetSize:contentMode:options:resultHandler:

Upvotes: 2

Related Questions