2011-03-19 117 views
4

我正在製作一個應用程序,用戶拍攝照片,將疊加圖像放置在圖像上,然後用戶可以保存圖像或將其上傳到Facebook或其他網站。將添加的疊加圖片保存到照片庫

我已經設法讓應用程序拍攝照片,並使覆蓋我使用UIImageView,它放置在照片的頂部。

我不確定我可以如何將圖像,覆蓋圖,一個文件,照片庫或Facebook導出。

+0

你有沒有得到答案呢? – LilMoke 2012-03-19 20:42:36

+0

是的,請參閱下面的答案。只需在您創建的imageview中繪製圖像,並且可以使用此方法從內容中製作UIImage。 – 2012-03-20 02:30:28

回答

2

這應該給你的想法。不過,這個代碼可能需要調整一兩個。 (我承認我沒有運行它。)

你的UIImageView的基本層可以將自己渲染成CoreGraphics位圖上下文(當然,這將包括所有子層),然後它可以爲您提供新的UIImage

UIImage *finalImage; 
// Get the layer 
CALayer *layer = [myImageView layer]; 
// Create a bitmap context and make it the current context 
UIGraphicsBeginImageContext(layer.bounds.size); 
// Get a reference to the context 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
// Ask the layer to draw itself 
[layer drawInContext:ctx]; 
// Then ask the context itself for the image 
finalImage = UIGraphicsGetImageFromCurrentImageContext(); 
// Finally, pop the context off the stack 
UIGraphicsEndImageContext(); 

這些功能都在UIKit Function Reference中描述。

還有,讓你喜歡的顏色過的東西更多的控制權,這包括創建一個CGBitmapContext並得到一個CGImageRef,從中可以再產生UIImage一個稍微複雜的方式。這在「Quartz 2D Programming Guide」部分"Creating a Bitmap Graphics Context"中有描述。

+1

請注意,如果要使用層次結構繪製視圖,則應使用CALayer類的renderInContext:方法。 – IlDan 2012-03-13 21:34:12