2012-03-15 112 views
1

使用本教程:http://www.techotopia.com/index.php/An_Example_iOS_4_iPhone_Camera_Application_%28Xcode_4%29 我在界面上顯示了圖片庫中的圖像。我想簡單地在UIImage上覆蓋一些基本文字或形狀(如方形)。是否有捷徑可尋?在圖像上顯示覆蓋圖

謝謝。

+0

疊加在UIImage的一個視圖。如果你想要文字,然後在圖像後添加一個UILabel,然後設置框架,使其覆蓋在正確的位置。對於形狀只需使用適當的視圖爲你的形狀,並做同樣的事情。 – Joe 2012-03-15 18:06:08

+0

@Joe我怎麼會在代碼上做到這一點?謝謝。 – Objc55 2012-03-15 18:08:22

+0

對不起,我忘了有些人只能使用界面構建器才能得到。 [查看iOS編程指南](https://developer.apple.com/library/ios/#DOCUMENTATION/WindowsViews/Conceptual/ViewPG_iPhoneOS/CreatingViews/CreatingViews.html#//apple_ref/doc/uid/TP40009503-CH5-SW6 ) – Joe 2012-03-15 20:11:46

回答

2

你只有UIImage還是使用UIImageView顯示它?

如果您有UIImageView,那麼您可以將UILabel作爲子視圖添加到圖像視圖中。

假設你的UIImageView被命名爲myImageView ...

// Create and init the label with any rect. 
UILabel *myLabel = [[UILabel alloc] initWithFrame:myImageView.frame]; // The lable has the same size as the Image has. 
mylabel.text = @"the text"; 
[myImageView addSubview:myLabel]; 
//A subview's coordinates are within its superview's coordinates. 
mylabel.center = CGPointMake(myImageView.frame.size.width/2,myImageView.frame.size.height/2) // aligning the centers of the views 
//An alternative could be transforming the image's center into the coordinate system of its superviews. That code would look smarter but his example is imho easier to understand. 

//Setting front, size, alpha, background colour ... 
... 
+0

我有一個imageView中顯示的圖像,像這樣:'UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage]; imageView.image = image;' – Objc55 2012-03-15 18:37:58

+0

好。在這種情況下,上面的代碼是可行的。正如你所看到的,它將把Lable放在ImageView的中心。隨意改變這一點。請記住,子視圖座標系的原點(0,0)位於其超視圖的左上角。當然,您可能想要更改顏色,對齊等其他屬性。 – 2012-03-15 18:42:44

+0

感謝您的回覆。不幸的是,使用你的代碼,文本確實會出現,但它應該出現在最後的圖像消失,並被替換,所以我最終只在白色背景上顯示黑色文本。 – Objc55 2012-03-15 18:47:45

0

您可以創建一個UIView的子類,使之透明,並添加一個實例這個類的,同樣大小的圖像,作爲覆蓋圖像頂部的子視圖。然後,當您想要在圖像上顯示某些內容時,請在透明子視圖上調用setNeedsDisplay,然後在該視圖的drawRect回調中繪製任何您想要的內容(彩色矩形,文本等)。

要使UIView子類透明的,將其添加到視圖類initWithFrame方法:

[ self setBackgroundColor:[ UIColor clearColor ] ];