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; }
博文作者:GarveyCalvin
博文出處:http://www.cnblogs.com/GarveyCalvin/
本文版權歸作者和博客園共有,歡迎轉載,但須保留此段聲明,並給出原文鏈接,謝謝合作!