Unable to extract JPEG from PHAsset
Unable to extract JPEG from PHAsset
- Subject: Unable to extract JPEG from PHAsset
- From: Glen Huang <email@hidden>
- Date: Fri, 16 Jun 2017 22:31:42 +0800
Hi,
I'm trying to extract the JPEG data from a PHAsset. I did it like this:
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key:
"creationDate", ascending: true)]
let assets = PHAsset.fetchAssets(with: fetchOptions)
jpg(for: assets[0])
where jpg(for:) is
func jpg(for asset: PHAsset) {
let size = CGSize(width: 100, height: 100)
PHImageManager.default().requestImage(for: asset, targetSize: size,
contentMode: .aspectFit, options: nil) { image, info in
if let image = image {
let jpg = UIImageJPEGRepresentation(image, 0.8)!
}
}
}
It works in the simulator running iOS 11, but fails on iPhone 5S every time with
fatal error: unexpectedly found nil while unwrapping an Optional value
And the thing caused that error was the bang after the
UIImageJPEGRepresentation function.
I'm using Xcode 9 beta, Swift 4, new build system, and the phone runs iOS 10.3.2
Any idea why that happens and how I can extract the JPEG data from PHAsset?
Thank you.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden