Created
May 12, 2017 06:50
-
-
Save sourleangchhean168/b91342610211f4c4eea0075837ab195d to your computer and use it in GitHub Desktop.
Convert PHAsset to UIImage: 3.0.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func getAssetThumbnail(asset: PHAsset, size: CGFloat) -> UIImage { | |
let retinaScale = UIScreen.main.scale | |
let retinaSquare = CGSize(width: size * retinaScale, height: size * retinaScale)//(size * retinaScale, size * retinaScale) | |
let cropSizeLength = min(asset.pixelWidth, asset.pixelHeight) | |
let square = CGRect(x:0, y: 0,width: CGFloat(cropSizeLength),height: CGFloat(cropSizeLength)) | |
let cropRect = square.applying(CGAffineTransform(scaleX: 1.0/CGFloat(asset.pixelWidth), y: 1.0/CGFloat(asset.pixelHeight))) | |
let manager = PHImageManager.default() | |
let options = PHImageRequestOptions() | |
var thumbnail = UIImage() | |
options.isSynchronous = true | |
options.deliveryMode = .highQualityFormat | |
options.resizeMode = .exact | |
options.normalizedCropRect = cropRect | |
manager.requestImage(for: asset, targetSize: retinaSquare, contentMode: .aspectFit, options: options, resultHandler: {(result, info)->Void in | |
thumbnail = result! | |
}) | |
return thumbnail | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment