iPhone屏幕捕获视图
在iphone上,是否有可能屏幕捕获一个UIView及其所有子视图?如果可能,怎么办?
On iphone, is it possible to "screen capture" an UIView and all its subview? If it is possible, how?
我发现了这个,但没有尝试过:
I found this, but haven't tried out myself:
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
Here you find the used -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];
I transformed the codes above to a category on UIView.
call it like this: [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);
}