iPhone UIImage loaded from core data is rotated counterclockwise by 90 degrees. How to fix?
core-data, ios, iphone, objective-c, uiimage
Solution
After a year you have probably solved this yourself, but I have experienced this problem myself recently and thought I would put up my solution.
I changed the stored image format to JPEG.
change
coreDataEntity.image = UIImagePNGRepresentation(imageView.image);
to
coreDataEntity.image = UIImageJPEGRepresentation(imageView.image,1.0f);
The image is restored as before
imageView.image = [UIImage imageWithData:coreDataEntity.image];
Hope this helps someone.
Problem
I'm capturing an image from camera using AVCaptureSession: ``` - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection ``` The image gets converted into a UIImage and is added to an image view. The image is displayed in a portrait orientation, as expected. I'm trying to save the image into core data between capture sessions. My core data entity has an image property with binary data type. ``` coreDataEntity.image = UIImagePNGRepresentation(imageView.image); ``` Once the user reloads a previously saved session, the image view has its image restored using: ``` imageView.image = [UIImage imageWithData:coreDataEntity.image]; ``` However, the restored image is rotated 90 degrees counterclockwise with it's top pointing to the left. At which point does the image get rotated? Is it during saving or during loading from core data? I have a method to rotate a UIImage by 90 degrees, but it appears that on subsequent re-loading of the same image, it gets rotated by extra 90 degrees. How can I know that I've already rotated a UIImage once? Thank you for any help!