Re: Unable to extract JPEG from PHAsset
Re: Unable to extract JPEG from PHAsset
- Subject: Re: Unable to extract JPEG from PHAsset
- From: Glen Huang <email@hidden>
- Date: Sun, 18 Jun 2017 17:45:13 +0800
Thanks for the reply.
I'm sorry but I'm not familiar with the "usual debugging things" that can be
apply to UIImage. Is there any official document introducing the inner workings
of UIImage that I can read?
But following your advice, I did go check image.cgImage before passing it to
UIImageJPEGRepresentation, and it was not nil. Not sure what that implies.
Also it seems a common problem according to
https://stackoverflow.com/questions/17440741/uiimagepngrepresentation-returns-nil-data
<https://stackoverflow.com/questions/17440741/uiimagepngrepresentation-returns-nil-data>
I tried the solution involving UIGraphicsBeginImageContext, and it works.
I also tried simulator running iOS 10 (without using the solution), and it
works too. It only fails when running on a real device.
What more can I do to find the real cause?
> On 18 Jun 2017, at 1:29 PM, David Duncan <email@hidden> wrote:
>
>
>> On Jun 16, 2017, at 7:31 AM, Glen Huang <email@hidden> 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 (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
>
> --
> David Duncan
>
_______________________________________________
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