从PHAsset获取缩略图图像

时间:2016-08-23 13:36:32

标签: objective-c phasset photosframework

我想获得PHAsset的缩略图。我已经从照片库中提取了一个PHAsset,现在想要获取缩略图。

你能帮我解决Objective-C吗?

谢谢!

4 个答案:

答案 0 :(得分:2)

如果有人正在寻找一个快速的解决方案,这里有一个扩展名:

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
        }
    }
}

答案 1 :(得分:1)

The PHImageManagerClass has the method:

- requestImageForAsset:targetSize:contentMode:options:resultHandler:

答案 2 :(得分:1)

  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];

    }];

我从How to fetch squared thumbnails from PHImageManager?

得到了这个答案

答案 3 :(得分:1)

以下是Swift 4的完整答案,显示了功能&打电话给它。此外,请确保您在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
})