怎麼樣用代碼寫出截取屏幕內容,並讓截取到的內容以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.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
怎樣在代碼中具體使用
假設self.view上面有一個firstImageView和一個secondImageView,我想截取firstImageView視圖的內容,保存在UIImage中,然後賦值給secondImageView.image
self.secondImageView.image = [self snapshot:self.firstImageView];