2012-07-15 104 views
0

如何從UIView子類創建CGImage?從UIView子類創建CGImage

我應該使用renderInContext:UIGraphicsGetCurrentContext()從包含繪圖(核心圖形通過drawRect方法)的視圖(UIView的子類)創建一個CG圖像?

我有一個視圖myView,其中我正在做一些繪圖。我想用我創建的繪圖做一些其他的核心圖形操作 - 比如混合模式等與導入的照片,所以我需要從我假設的myView獲取一個CGimage。找不到CGImage屬性(我的第一個猜測)。我周圍發現瞭解決方案,但是我的子類「沒有聲明選擇器renderInContext」,所以我得到一個錯誤。

UIGraphicsBeginImageContext(myView.bounds.size); 
[myView renderInContext:UIGraphicsGetCurrentContext()]; 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

CGImageRef cgImage = image.CGImage; 
+0

相似問題:[爲什麼我會得到'No -renderInContext:method found'警告?](http://stackoverflow.com/questions/4770544/why-do-i-get-no-renderincontext-method-found -警告) – 2012-07-15 05:14:47

回答

1

-renderInContext:CALayer,不UIView的方法。

而是執行此操作:

[myView.layer renderInContext:UIGraphicsGetCurrentContext()]; 

您還需要導入頭爲CALayer

#import <QuartzCore/QuartzCore.h> 

,反對QuartzCore框架鏈接您的應用程序。