iOS对UIView对象或子类对象截屏(截取的内容以UIImage的形式存储)

来源:互联网 发布:mybatis 多个数据库 编辑:程序博客网 时间:2024/05/02 01:18

     怎么样用代码写出截取屏幕内容,并让截取到的内容以UIImage的形式存储显示?从iOS7开始,UIView就提供了一个方法:drawViewHierarchyInRect:afterScreenUpdates:,它允许截取UIView或者其之类中的内容,并且以位图的形式保存到UIImage中,包括UIkit,quartz,OpenGL ES,SpriteKit等。

#pragma mark - snapshot

- (UIImage *)snapshot:(UIView *)view

{

    UIGraphicsBeginImageContextWithOptions(view.bounds.size,YES, 0);

    [view drawViewHierarchyInRect:view.boundsafterScreenUpdates:YES];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    

    return image;

}

怎样在代码中具体使用

假设self.view上面有一个firstImageView和一个secondImageView,我想截取firstImageView视图的内容,保存在UIImage中,然后赋值给secondImageView.image

self.secondImageView.image = [self snapshot:self.firstImageView];


0 0
原创粉丝点击