iOS 7截取部分UIView的截图 [英] iOS 7 taking screenshot of part of a UIView

查看:428
本文介绍了iOS 7截取部分UIView的截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个400x320的视图(testView),我需要截取该视图的部分截图(例如rect =(50,50,200,200))。
我正在玩iOS 7中的drawViewHierarchy方法,但我无法弄清楚如何正确地完成它。

I have a view (testView) that is 400x320 and I need to take a screenshot of part of this view (say rect = (50, 50, 200, 200)). I am playing around with the drawViewHierarchy method in iOS 7 but I can't figure out how to do it correctly.

    UIGraphicsBeginImageContextWithOptions(self.testView.bounds.size, NO, [UIScreen mainScreen].scale);

    [self.testView drawViewHierarchyInRect:self.testView.bounds afterScreenUpdates:YES];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

任何帮助将不胜感激!

谢谢。

推荐答案

获取整个快照后,您可以在较小的图形上下文中以一种获得该部分的方式绘制它你想要的:

After getting the whole snapshot, you could draw it in a smaller Graphic Context in a way that you get the part you want:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), YES, [UIScreen mainScreen].scale);
[image drawInRect:CGRectMake(-50, -50, image.size.width, image.size.height)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

编辑:更好的是,直接在较小的上下文中绘制层次结构:

Better yet, draw the hierarchy directly in that smaller context:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), NO, [UIScreen mainScreen].scale);
[self.testView drawViewHierarchyInRect:CGRectMake(-50, -50, self.testView.bounds.size.width, self.testView.bounds.size.height) afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

这篇关于iOS 7截取部分UIView的截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆