> On Jun 16, 2017, at 7:31 AM, Glen Huang <hey...@gmail.com> wrote: > > 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?
There is no obvious reason why this should fail, but the usual debugging things apply – what are the quality of image? (image.CGImage can still be nil, even if image is not). Does UIImageJPEGRepresentation() fail in any other situations? Can you reproduce this without using PHAsset? > > Thank you. > _______________________________________________ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > 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: > https://lists.apple.com/mailman/options/cocoa-dev/david.duncan%40apple.com > > This email sent to david.dun...@apple.com -- David Duncan _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com