Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS 7拍摄部分UIView的屏幕截图_Ios_Objective C_Uiview_Ios7 - Fatal编程技术网

iOS 7拍摄部分UIView的屏幕截图

iOS 7拍摄部分UIView的屏幕截图,ios,objective-c,uiview,ios7,Ios,Objective C,Uiview,Ios7,我有一个视图(testView),它是400x320,我需要截取这个视图的一部分(比如rect=(50,50,200,200))。 我在iOS 7中使用drawViewHierarchy方法,但我不知道如何正确地使用它 UIGraphicsBeginImageContextWithOptions(self.testView.bounds.size, NO, [UIScreen mainScreen].scale); [self.testView drawViewHierarch

我有一个视图(testView),它是400x320,我需要截取这个视图的一部分(比如rect=(50,50,200,200))。 我在iOS 7中使用drawViewHierarchy方法,但我不知道如何正确地使用它

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

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

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
任何帮助都将不胜感激


谢谢。

获取整个快照后,您可以在较小的图形环境中绘制快照,从而获得所需的零件:

UIGraphicsBeginImageContextWithOptions(CGSizeMake(200, 200), YES, [UIScreen mainScreen].scale);
[image drawInRect:CGRectMake(-50, -50, image.size.width, image.size.height)];
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
编辑:更好的是,直接在较小的上下文中绘制层次结构:

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

请在下面的代码行中尝试

UIView *popSnapshot=[inputView snapshotViewAfterScreenUpdates:YES];

非常感谢。这对我帮助很大!在Swift中使用
self.hostView.superview!。drawHierarchy(in:frame,afterScreenUpdate:true)
其中
frame
肯定比
superView
小,我仍然可以获得完整视图的内容。但是它被压缩成了我提供给
UIGraphicsBeginImageContextWithOptions()
(我当然把它和我的
frame
size
)的
CGSize
)的
CGSize
。@重要的是,这里的诀窍是使用完全相同的视图大小来绘制层次结构(防止缩放),然后偏移它,得到你想要的零件。通过使用
UIGraphicsBeginImageContextWithOptions(CGSize(width:200,height:200),true,0)
您可以使用
view.drawHierarchy(in:view.bounds,afterScreenuUpdate:true)获得视图的左上角200x200部分。
。您还可以通过以下方式偏移零件:
view.drawHierarchy(in:uiedgeInSetRect(view.bounds,UIEdgeInsets(top:-100,left:-100,bottom:0,right:0)),AfterScreenUpdate:true)
。希望有帮助!