0

I am cropping a UIImage and trying to preserve its orientation. However when I use its original orientation, 0, the new UIImage is incorrectly rotated.

//_picture is a UIImageView and .image gives the UIImage

    NSLog(@"orientation ORIGINAL %i",_picture.image.imageOrientation); //logs out 0
    CGRect rect = CGRectMake(0,0,300,300);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef ref = [context createCGImage:_pictureFilterImage fromRect:rect];
    _picture.image = [UIImage imageWithCGImage:ref scale:1.0 orientation:_picture.image.imageOrientation];
    [_picture setFrame:rect];
    NSLog(@"orientation is now %i",_picture.image.imageOrientation); //logs out 0 again

However, if I hardcode in 3 in place of _picture.image.imageOrientation it gives me the right orientation (for the specific way the picture is taken but clearly wont work if the user holds their device differently).

Why does this not give me the right image? Suggestions on how to preserve the orientation and why using the original orientation might not be working?

FYI: When I log out the original orientation it gives me a 0. When I hardcode in 3, it of course logs out 3 but the new image is now the same orientation as it was before. Why does it need to go from 0 to 3 to preserve orientation, what am I missing?

Aggressor
  • 13,323
  • 24
  • 103
  • 182
  • Did you check the following URL [Link](http://stackoverflow.com/questions/7253270/how-to-crop-the-uiimage-in-iphone) – Bharath Sep 27 '14 at 07:34
  • It had to do with orientation being lost when I changed coordinate matrix. I needed to use a CGAffineTransform to rotate the CIImage after I applied a filter to it. – Aggressor Sep 27 '14 at 13:35

0 Answers0