1

Really hope you can help on this one been bugging me all day

Is it possible to keep the orienation of a UIImage (Say Landscape) but rotate the Image inside?

I save my UIImage using the following Code

[UIImagePNGRepresentation(image) writeToFile:pngPath atomically:YES];

where image is a UIImage.

The picture below shows what gets saved depending on the orientation of the device.

the bottom 4 is what I am Trying to achieve

Orientation Image

Basically I am trying to get the pictue to be on its side in landscape mode no matter which way you turn the device

Any help is appreciated

Mark

Mark A Barr
  • 77
  • 1
  • 10

4 Answers4

0

Try this

UIImage* landscapeImage = [UIImage imageWithCGImage:image.CGImage
                                            scale:image.scale
                                      orientation:UIImageOrientationLeft];
[UIImagePNGRepresentation(landscapeImage) writeToFile:pngPath atomically:YES];
Surya Subenthiran
  • 2,217
  • 1
  • 15
  • 22
  • Sorry It Does nothing I applied it to the origianl image which is landscape with the image inside upside down and it cam back with the same image i need it to turn right 90 degrees but stay landscape – Mark A Barr Apr 18 '16 at 20:15
  • The above code only changes any orientation to Landscape left. If you are talking about rotating image then refer the link http://stackoverflow.com/questions/11667565/how-to-rotate-an-image-90-degrees-on-ios – Surya Subenthiran Apr 18 '16 at 20:29
  • Thanks I saw that one long with about another 100 the all rotate the image to what I want but all either rotate the orientation as well or squashes the image to fit – Mark A Barr Apr 18 '16 at 21:41
  • I think I might be able to modify one on the link you gave called RotateImage which takes the image and rotation just need help in working out how to crop the image to fit landscape – Mark A Barr Apr 18 '16 at 21:43
  • I Think its these two lines that are the key UIView *rotatedViewBox = [[UIView alloc] initWithFrame:CGRectMake(0,0,image.size.width, image.size.height)]; CGContextDrawImage(bitmap, CGRectMake(-image.size.width, -image.size.height, image.size.width, image.size.height), [image CGImage]); i think the first one sets the area to be grabbed from the original image and the second line draws it to the new image but cant work out how to do it – Mark A Barr Apr 18 '16 at 21:54
  • tried setting all to 960 square but the resultant image looks squashed still – Mark A Barr Apr 18 '16 at 21:55
0

I would just save the image file normally then change the imageView in the UI. As long as the image is always in portrait mode first you could just rotate the image view by 90 degrees like this:

self.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);

then set the imageView mode to Aspect Fit

self.imageView.contentMode = UIViewContentModeScaleAspectFit;

However, this will not work if your image needs to rotate the other way or if it is already positioned in landscape.

Bryan Norden
  • 2,397
  • 18
  • 22
0

Does it help to fix the orientation of taken images?

https://stackoverflow.com/a/15039536

Maybe you can find inspiration for your own problem if not. Basically create context with right width/height, transform for rotation, draw image, store as new image.

CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
    CGImageGetBitsPerComponent(image.CGImage), 0,
    CGImageGetColorSpace(image.CGImage),
    CGImageGetBitmapInfo(image.CGImage));
    CGContextConcatCTM(ctx, transform);
CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
//or
CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.CGImage);

CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgimg];

Also read comments about different results from camera and gallery.

If you only want to show it rotated, you can transform the UIView like @bryannorden suggests.

Community
  • 1
  • 1
  • have embedded your image, now that i see it, you always want the person in portrait, no matter what orientation it was taken in. So you will need to crop the landscape images before (or after rotation). What you could also do is change the CGRectMake in my example, just start at negative coordinates and make it draw outside of the canvas. – user6227617 Apr 20 '16 at 09:05
  • if you work with ImageView, you will need clipping, see here: http://stackoverflow.com/questions/6638623/uiviewcontentmodescaleaspectfill-not-clipping – user6227617 Apr 20 '16 at 09:06
0
NSBitmapImageRep *bit_map = [[NSBitmapImageRep alloc]
                             initWithBitmapDataPlanes:NULL
                             pixelsWide:img_size.width
                             pixelsHigh:img_size.height
                             bitsPerSample:8
                             samplesPerPixel:4
                             hasAlpha:YES
                             isPlanar:NO
                             colorSpaceName:NSDeviceRGBColorSpace
                             bitmapFormat:NSAlphaFirstBitmapFormat
                             bytesPerRow:0
                             bitsPerPixel:0];
NSAffineTransform *trans2=[NSAffineTransform transform];
[trans2 translateXBy:dirtyRect.size.width/2 yBy:dirtyRect.size.height/2];
[trans2 rotateByDegrees: angle];
[trans2 concat];

NSGraphicsContext *g=[NSGraphicsContext graphicsContextWithBitmapImageRep:bit_map];
[NSGraphicsContext saveGraphicsState];
[NSGraphicsContext setCurrentContext:g];
[img drawInRect:NSMakeRect(0, 0, img_size.width, img_size.height)];
[NSGraphicsContext restoreGraphicsState];


[bit_map drawInRect:NSMakeRect(-img_size.width/2, -img_size.height/2, img_size.width, img_size.height)];

see it in action: https://www.youtube.com/watch?v=DD_KYaq3SSg