iPhone横屏视图截图

问题描述:

我在横向模式下运行应用程序。 我必须采取什么措施才能使拍摄的屏幕也处于横向模式。iPhone横屏视图截图

我在iphone应用程序中使用下面的代码与底部的tabbar和采取屏幕截图,但其始终只在肖像模式。为什么?

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

UIGraphicsBeginImageContext(screenWindow.frame.size); 
[screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

我以前的答案是不正确的。我已经在这方面做了工作,我有一个风景右侧屏幕截图的答案。这将为相机胶卷正确保存正确的风景画面。我还没有尝试左风景。

这是在另一个计算器问题here中提供的答案的变体。该答案的问题是屏幕截图始终有一个UIImageOrientationUp的imageOrientation(只读)。因此,我们需要知道方向是什么,并适当地改变它。

我希望这会有所帮助。如果这适用于左侧风景,或者您获得了左侧风景的变体,请发布。

- (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; 
} 
+0

我想,也和它没有工作 – Satyam 2010-08-20 05:17:17

+1

Satyman更换UIImageOrientationLeft,为你做这个更新的答案的工作?还是你走向另一个方向? – dredful 2010-09-13 18:50:31

这是一个非常老的线程,但这里是如何做到这一点,以防万一任何人也有这个问题。

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]; 

如果你想让它旋转的权利,只是UIImageOrientationRight

+0

有帮助的答案。谢谢。 – Vignesh 2013-08-27 21:56:50