IOS截屏

最近,在找IOS截屏的方法。找到一个,现在记录下来。跟我想的差不多,还是,进入操作(初始化),操作(复制当前屏幕,保存),退出。最小的操作结构。

1 . 先指定图像的大小

UIGraphicsBeginImageContext(view.frame.size);

2. 在指定的区域绘制图像

[view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];

3. 获取图像上下文

UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

4. 关闭图像上下文

UIGraphicsEndImageContext();

没错!只需要4行代码就可以获取到指定View的图像截图。

 

另外,常用的绘制图像还有另一个方法:

[view.layer renderInContext:UIGraphicsGetCurrentContext()];

 

+ (UIImage *)getImageViewWithView:(UIView *)view
{
    UIGraphicsBeginImageContext(view.frame.size);
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:NO];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return image;
}

 代码还是对现实世界的一个简单映射啊。只能做简单重复的操作。影响开发能力的还是人自己啊。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。