2011-10-25 67 views

回答

8
UIImageView *someImageView = [[UIImageView alloc] initWithFrame:someRect]; 
someImageView.image = someImage; 
[self.view addSubview:someImageView]; 

容易餡餅! :d

您可能要檢查蘋果文檔太: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html

+0

這應該是'UIImageView * someImageView'和'someImageView.image = someImage;'? – DelightedD0D

+0

好吧,我是該死的。是的,它應該有xD複製代碼和調整到一般片段似乎很難xD –

3
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 7, 190, 23]; 
tabNameBackGround.image = [UIImage imageNamed:@"a.png"]; 
[self.view addSubview:imageView]; 
19

可以seperately創建一個UIImage,並從您的UIImage創建您的UIImageView。

UIImage *myImage = [UIImage imageNamed:@"great_pic.png"]; 
UIImageView *myImageView = [[UIImageView alloc] initWithImage:myImage]; 

然後,設置您的UIImageView

[myImageView setFrame:CGRectMake(0, 0, 100, 200)]; 

做任何你想要與你的圖像的大小,但不要忘了將其釋放。

[anotherView addSubview:myImageView]; 
[myImageView release]; 
+4

如果在啓用了ARC(自動引用計數)的Xcode 4.2中開發,則不需要(確實,您不必)執行[myImageView發佈] ;因爲ARC會爲您處理對象的發佈。 –

4

答案到目前爲止比他們需要更復雜的:initWithImage:「調整接收器的框架以匹配指定的圖像大小」因此,所有你需要的是

UIImageView* v = [[UIImageView alloc] initWithImage: someImage];