用于视图的iPhone屏幕截图

hug*_*gie 6 cocoa-touch objective-c ios

在iPhone上,是否可以"屏幕捕获"UIView及其所有子视图?如果有可能,怎么样?

Vik*_*ica 9

我找到了这个,但还没试过自己:

http://reusablesnippets.posterous.com/capture-uiview

在这里您可以找到使用的 -renderInContext http://developer.apple.com/mac/library/documentation/GraphicsImaging/Reference/CALayer_class/Introduction/Introduction.html#//apple_ref/doc/uid/TP40004500-CH1-SW120

编辑

我将上面的代码转换为UIView上的一个类别.这样叫:[aView saveScreenshotToPhotosAlbum];

#import <QuartzCore/QuartzCore.h>

- (UIImage*)captureView {

    CGRect rect = [[UIScreen mainScreen] bounds];  
    UIGraphicsBeginImageContext(rect.size);     
    CGContextRef context = UIGraphicsGetCurrentContext();  
    [self.layer renderInContext:context];  
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();  
    UIGraphicsEndImageContext();  
    return img;
}



- (void)saveScreenshotToPhotosAlbum {
    UIImageWriteToSavedPhotosAlbum([self captureView], nil, nil, nil);

}
Run Code Online (Sandbox Code Playgroud)