4

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!

Alex Stone
  • 46,408
  • 55
  • 231
  • 407

5 Answers5

11

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.

dpetz
  • 126
  • 1
  • 3
5

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.

Sean Freitag
  • 930
  • 1
  • 6
  • 15
  • Can you give an example of how to encapsulate with NSValueTransformer? I don't really understand that step. – zakdances Jun 08 '12 at 18:04
2

use

let imageData = image.jpegData(compressionQuality: 1)

instead of

let imageData = image.pngData()
Akash Kundu
  • 1,278
  • 13
  • 21
1

My guess is that it's neither during the load or save. I find it suspicious that you have a method that rotates the image by 90 degrees and that's just what's happening to your image. Try modifying your method to rotate by a different angle (zero for example) and see of it affects your bug. I suspect your rotate method is called and you don't know it.

Another possibility is that you hold your iPhone/iPad in landscape when you take your photo but your app does not support device rotation.

There's really not much info in your post so I'm just fishing here :)

mprivat
  • 21,582
  • 4
  • 54
  • 64
  • Thank you for a suggestion! I got the rotation method after I discovered that bug, so it's not the fault of the rotation method. Rather I wanted to use it to remedy the issue once I found the root cause. – Alex Stone Apr 21 '12 at 15:18
1

Swift 4 > : before saving the image as pngData() call this function and use the returned image here:

 func rotatedCopy() -> UIImage {
    if self.imageOrientation == UIImage.Orientation.up {
        return self
    }

    UIGraphicsBeginImageContext(size)

    //draws the image in current context respecting orientation
    draw(in: CGRect(origin: CGPoint.zero, size: size))
    let copy = UIGraphicsGetImageFromCurrentImageContext()

    UIGraphicsEndImageContext()

    return copy!
 }
arvinq
  • 656
  • 6
  • 12