2013-07-16 39 views
-2

我获取该代码来自苹果文档...iOS版 - 以截图与状态栏

+ (UIImage *)screenshot { 
    CGSize imageSize = [[UIScreen mainScreen] bounds].size; 
    if (NULL != UIGraphicsBeginImageContextWithOptions) { 
     UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0); 
    } else { 
     UIGraphicsBeginImageContext(imageSize); 
    } 

    CGContextRef context = UIGraphicsGetCurrentContext(); 

    for (UIWindow *window in [[UIApplication sharedApplication] windows]) { 
     if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) { 

      CGContextSaveGState(context); 

      CGContextTranslateCTM(context, [window center].x, [window center].y); 

      CGContextConcatCTM(context, [window transform]); 

      CGContextTranslateCTM(context, 
            -[window bounds].size.width * [[window layer] anchorPoint].x, 
            -[window bounds].size.height * [[window layer] anchorPoint].y); 


      [[window layer] renderInContext:context]; 


      CGContextRestoreGState(context); 
     } 
    } 


    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 

    UIGraphicsEndImageContext(); 
    return image; 
} 

不过,我有一个小问题吧。我想在我的屏幕截图中包含状态栏。我该怎么做呢?还是有更好的方法来做到这一点?

-Henry

+0

@livingtech Ooops。抱歉。让我看看。 –

回答

-1

这里是我结束了这样的方式......

+ (UIImage *)screenshot { 

    CGImageRef screen = UIGetScreenImage(); 
    UIImage *image = [UIImage imageWithCGImage:screen]; 
    CGImageRelease(screen); 

    return image; 
} 

虽然它不是应用程序商店可批准它要做到这一点的最好办法。

+1

这是一个了不起的方式,但是UIGetScreenImage是一个私有API,所以App Store不允许它?我们如何使用它来获取指定的区域截图? – Suge