iOS開發-簡單獲取View截圖圖像(Quartz2D)


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/

本文版權歸作者和博客園共有,歡迎轉載,但須保留此段聲明,並給出原文鏈接,謝謝合作!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM