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!
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.
I wouldn't force a format in the database. By converting to a PNG before storing it in the database, you are removing information that is probably needed (like orientation).
If my hypothesis is correct, try to serialize the UIImage like this instead:
[NSKeyedArchiver archivedDataWithRootObject:image]
and deserialize with this
[NSKeyedUnarchiver unarchiveObjectWithData:imageData]
Also, be sure to check out NSValueTransformer
, which can encapsulate this conversion so that you never have to deal with it once it's working.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With