2010-05-18 215 views
4

我在橫向模式下運行應用程序。 我必須採取什麼措施才能使拍攝的屏幕也處於橫向模式。iPhone橫屏視圖截圖

我在iphone應用程序中使用下面的代碼與底部的tabbar和採取屏幕截圖,但其始終只在肖像模式。爲什麼?

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

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

回答

2

我以前的答案是不正確的。我已經在這方面做了工作,我有一個風景右側屏幕截圖的答案。這將爲相機膠捲正確保存正確的風景畫面。我還沒有嘗試左風景。

這是在另一個計算器問題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

1

這是一個非常老的線程,但這裏是如何做到這一點,以防萬一任何人也有這個問題。

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