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
.