本文介绍了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:

在这里您可以找到使用的 -renderInContext:

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);

}

这篇关于iPhone屏幕捕获视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:49
查看更多