以编程方式屏幕截取Retina /高分辨率屏幕

时间:2010-12-26 13:27:37

标签: iphone cocoa-touch jpeg screen-capture

亲爱的学者们 我使用以下代码捕获屏幕并将其保存到相册内的jpg - 这非常有用。

然而,当在具有更高分辨率的iPhone 4上运行时,捕获的屏幕仅为320X480而不是更高的分辨率(我认为在iPad上也是如此)。

我应该如何解决这个问题?

// Save the captured image to photo album
- (IBAction)saveAsJPG
{

    UIImage *image = [self captureView:self.view];  
    UIImageWriteToSavedPhotosAlbum(image, self, 
           @selector(image:didFinishSavingWithError:contextInfo:), nil);
}

-(UIImage *)captureView:(UIView *)view 
{ 
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    UIGraphicsBeginImageContext(screenRect.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    [[UIColor blackColor] set]; 
    CGContextFillRect(ctx, screenRect);
    [view.layer renderInContext:ctx];   
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage; 
}

1 个答案:

答案 0 :(得分:15)

使用UIGraphicsBeginImageContextWithOptions代替UIGraphicsBeginImageContext

UIGraphicsBeginImageContextWithOptions(screenRect.size, NO, 0.0);

有关详细信息,请参阅Apple QA1703