2011-12-22 84 views
1

我正在構建一個簡單的應用程序,允許用戶拍攝將保存在相冊中的照片。因爲我希望用戶能夠拍攝大量看起來相似但不相同的物體的圖像(例如,房屋中的傢俱的細節),所以我想在圖像上添加一個小文本標題來描述圖像的內容。iOS 5:爲相機圖像添加標題

不用擔心用戶如何在此刻輸入此文本,任何人都可以給我一些建議,以瞭解如何將一些文本添加到使用UIImagePickerController拍攝的圖像中?

+0

你的意思是你想畫一些文字上的形象呢?或者向圖片的元數據添加文字? – 2011-12-22 08:16:37

+0

是的,在圖片上繪製文字。 – futureshocked 2011-12-22 10:10:43

回答

2
  1. 創建一個新的圖形上下文。
  2. 將圖像繪製到上下文中。
  3. 在上面畫上文字。
  4. 從上下文創建新圖像。

代碼應該是這樣的:

UIGraphicsBeginImageContextWithOptions(sourceImage.size, YES, sourceImage.scale); 
[sourceImage drawAtPoint:CGPointZero]; 

NSString *caption = @"Hello World"; 
UIFont *captionFont = [UIFont boldSystemFontOfSize:24.0]; 
[[UIColor whiteColor] setFill]; // or setStroke? I am not sure. 
[caption drawAtPoint:CGPointMake(10.0f, 10.0f) withFont:captionFont]; 

UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 
+0

太容易了,謝謝! – futureshocked 2011-12-23 00:21:31