Abhishek Kgsk
Abhishek Kgsk

Reputation: 41

Taking Screen Shot When App is in Landscape Mode

I am working on an app which will only be in landscape. In that app I have a functionality that is take screenshot of that particular screen. I have implemented this code

UIGraphicsBeginImageContext(self.view.window.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImageView *imageView = [[UIImageView alloc] init];
imageView.image = UIGraphicsGetImageFromCurrentImageContext();;
imageView.transform = CGAffineTransformMakeRotation(3.0 * M_PI / 2.0);
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(imageView.image, nil, nil, nil);

Using this code I get screen shot of my app in portrait mode. I want it in landscape mode.

Upvotes: 1

Views: 1655

Answers (2)

DrDisc
DrDisc

Reputation: 281

Perhaps late, but wrapping it in an UIImageView doesn't actually rotate the image itself. Here's some code I wrote that creates a rotated image from the full window of your application.

UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];

CGRect rect = [keyWindow bounds];
UIGraphicsBeginImageContext(rect.size);

CGContextRef context = UIGraphicsGetCurrentContext();
[keyWindow.layer renderInContext:context];

UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return [[UIImage alloc] initWithCGImage:[image CGImage] scale:1 orientation:UIImageOrientationLeft];

If you want it rotated right, just replace UIImageOrientationLeft with UIImageOrientationRight

Upvotes: 0

sunkehappy
sunkehappy

Reputation: 9091

Don't add that window. If you add it your context size is wrong.

// Just use self.view.bounds.size is OK
UIGraphicsBeginImageContext(self.view.bounds.size);

Upvotes: 6

Related Questions