0

I have an array of UIImages I am converting to PNG with UIImagePNGRepresentation. The UIImages are loaded from PHAssets from my photos library. Most convert without a problem, except for a few that will cause it to crash with the message:

fatal error: unexpectedly found nil while unwrapping an Optional value

Printing the UIImages to the console reveals this difference between those that don't cause a crash and those that do:

result #0: <UIImage: 0x13a46c5b0>, {60, 45}  // this works
thumbnail #0 finished loading from lib
result #1: <UIImage: 0x13b133b00>, {60, 45}  // this too
thumbnail #1 finished loading from lib
result #2: <UIImage: 0x13a46d440>, {45, 60}  // this one causes a crash
thumbnail #2 finished loading from lib
result #3: <UIImage: 0x13a444790>, {60, 45}  // this one works

See the pattern? What do the numbers in the brackets mean? Any ideas on a fix? Thanks!

UPDATE
I separated the unwrapping array[i] as! UIImage from the conversion and it works without a problem. It's definitely the conversion that's throwing the nil unwrapping error, even if I switch it to UIImageJPEGRepresentation.

noizybrain
  • 155
  • 1
  • 15
  • Maybe portrait and landscape. You need to show how you are converting it, you are probably forcing to unwrap your object. the culprit is the exclamation mark – Leo Dabus Dec 26 '15 at 14:07
  • Yes, this is how I'm converting it: UIImagePNGRepresentation(image!). But why would portrait/landscape make one of them nil of all things? The image still loads. – noizybrain Dec 26 '15 at 14:27
  • guard let image = image else { return } – Leo Dabus Dec 26 '15 at 14:43
  • Sure, if I'm expecting I might not get an image, but in this case I know that it's loading and it's there in the array. – noizybrain Dec 26 '15 at 14:57
  • sorry buddy but all I can say is that info looks like the image aspect ratio. And generally when saving PNG you may need to rotate the image prior to saving it – Leo Dabus Dec 26 '15 at 15:03

0 Answers0