2012-07-12 75 views
-4

我想設置圖像以在NSView的屏幕上顯示,但是以編程方式將圖像設置爲NSView

如何以編程方式在NSView內加載/設置圖像?

我曾嘗試:

[test setImage:[CIImage imageNamed: @"test2.png"]]; 
+0

你試過了,發生了什麼?什麼是「測試」?你爲什麼使用'CIImage'(它沒有'+ imageNamed:'方法,BTW)? – 2012-07-12 18:05:58

+0

測試是:IBOutlet NSView *測試;鏈接到nsview,沒什麼說:Class method'+ imageNamed:'not found(返回類型默認爲'id') – user1508409 2012-07-12 18:07:25

回答

-3

讓我們希望,該變種「測試」是的UIImageView的一個實例。如果是這種情況,你的嘗試將走在正確的軌道上。我會推薦使用UIKit框架。你將能夠完成你想要的使用:

//A custom size for your image view 
//**You will have to determine these four struct values yourself** 
CGRect frame = CGRectMake(x-origin, y-origin, width, height); 

//Creating the image view 
UIImageView *test = [[UIImageView alloc] initWithFrame:frame]; 

//Creating the image that will go in the image view 
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"test2" ofType:@"png"]]; 

//Setting the imageview's image property to the previously created image. 
test.image = image; 

//And then add the image view as a subview the current view 
[self.view addSubview:test]; 
+2

這不是iOS問題 - _NS_ImageView和[cocoa]標記。 – 2012-07-12 18:34:00

+0

我想使用:UIKit框架,但我只能在爲iPhone製作應用程序時包含它,因爲UIKit僅適用於iPhone/iPad,但是如何將其包含在我的Mac應用程序中? – user1508409 2012-07-12 18:41:40

相關問題