Landscape iPhone Shots

I run the application in landscape mode. What caution should I take to ensure that the captured image is also in landscape mode.

I use the following code in an iphone application with a tab at the bottom and take a screenshot, but always in portrait mode. why?

UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow]; UIGraphicsBeginImageContext(screenWindow.frame.size); [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); 
+4
source share
2 answers

My previous answer was wrong. I have been working on this and I have an answer to the right screenshot. This will save the right right screen shot in the camera roll. I have not tried the landscape.

This is a variant of the answer presented in another question here . The problem with this answer is that there is always an image on the screen. Orientation (read-only) of UIImageOrientationUp. Therefore, we must know what the orientation was and change it accordingly.

Hope this helps. If it works on the landscape on the left or you get a variation that works on the left edge, please post it.

  - (IBAction)cameraButtonClick:(id)sender { UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow]; UIGraphicsBeginImageContext(screenWindow.frame.size); [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()]; UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); UIImageWriteToSavedPhotosAlbum([self scaleAndRotateImage:screenshot], nil, nil, nil); UIGraphicsEndImageContext(); } // Code from: http://discussions.apple.com/thread.jspa?messageID=7949889 - (UIImage *)scaleAndRotateImage:(UIImage *)image { int kMaxResolution = 640; // Or whatever CGImageRef imgRef = image.CGImage; CGFloat width = CGImageGetWidth(imgRef); CGFloat height = CGImageGetHeight(imgRef); CGAffineTransform transform = CGAffineTransformIdentity; CGRect bounds = CGRectMake(0, 0, width, height); if (width > kMaxResolution || height > kMaxResolution) { CGFloat ratio = width/height; if (ratio > 1) { bounds.size.width = kMaxResolution; bounds.size.height = roundf(bounds.size.width / ratio); } else { bounds.size.height = kMaxResolution; bounds.size.width = roundf(bounds.size.height * ratio); } } CGFloat scaleRatio = bounds.size.width / width; CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef)); CGFloat boundHeight; boundHeight = bounds.size.height; bounds.size.height = bounds.size.width; bounds.size.width = boundHeight; transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width/2.0); transform = CGAffineTransformRotate(transform, M_PI / 2.0); UIGraphicsBeginImageContext(bounds.size); CGContextRef context = UIGraphicsGetCurrentContext(); UIImageOrientation orient = image.imageOrientation; if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) { CGContextScaleCTM(context, -scaleRatio, scaleRatio); CGContextTranslateCTM(context, -height, 0); } else { CGContextScaleCTM(context, scaleRatio, -scaleRatio); CGContextTranslateCTM(context, 0, -height); } CGContextConcatCTM(context, transform); CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef); UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return imageCopy; } 
+2
source

This is a really old thread, but here's how to do it now if someone has this problem too.

 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 to turn right, just replace UIImageOrientationLeft with UIImageOrientationRight

+1
source

Source: https://habr.com/ru/post/1310113/


All Articles