2013-03-13 97 views
-1
NSImageCell *imageFromBundle;  
NSData *imageData;  
NSURL *imageURL = [NSURL URLWithString:@"http://www.greatwhatsit.com/wp-content/uploads/2012/03/jeremiah1.jpg"];  
NSLog(@"url");  
NSLog(@"data");  
NSImage *imageFromBundle = [[NSImage alloc] initWithData:imageData]; 

爲什麼這段代碼沒有工作。的Xcode 4.6爲什麼此代碼不斷出現錯誤。 xCode 4.6

+0

談到了「與不同類型的‘imageFromBundle’的重新定義:‘NSImage中* _strong’VS‘NSImageCell * _strong’究竟發生了什麼,我已經改變了一切,它仍然來了同樣的錯誤 – user2164473 2013-03-13 08:45:36

回答

1

還需要初始化的imageData:

imageData = [NSData dataWithContentsOfURL:imageURL]; 
NSImage *imageFromBundle = [[NSImage alloc] initWithData:imageData]; 
1

你必須擁有的imageFromBundle

NSImageCell *imageFromBundle; 
NSImage *imageFromBundle = [[NSImage alloc] initWithData:imageData]; 

變化的定義和初始化同類型:

NSImage *imageFromBundle; 
NSImage *imageFromBundle = [[NSImage alloc] initWithData:imageData]; 

此外,作爲zakhej sais,您需要初始化imageData

+0

想起imageFromBundle的重新定義?我剛剛開始編程昨天,對不起。 – user2164473 2013-03-13 08:53:54

+0

首先嚐試用'NSImage'替換'NSImageCell'。這應該可以解決你所描述的錯誤。 – Sunkas 2013-03-13 09:33:26

0

嘗試

NSURL *imageURL = [NSURL URLWithString:@"http://www.greatwhatsit.com/wp-content/uploads/2012/03/jeremiah1.jpg"];  
NSData *imageData = [NSData dataWithContentsOfURL:imageURL]; 
NSLog(@"url");  
NSLog(@"data");  
NSImage *imageFromBundle = [[NSImage alloc] initWithData:imageData]; 
+0

非常感謝。我現在需要做的就是創建正確的我,如果我錯了一個NSObject * imageFromBundle爲了加載變量,它說它是未使用的變量* imageFromBundle。 – user2164473 2013-03-13 08:59:20

+0

根據你的代碼,你在NSImage&NSImageCell的兩個地方聲明imageFromBundle。被你使用d一個出來。 – Girish 2013-03-13 09:05:48

0

imageFromBundle是第一個問題,因爲已經指出。 但存在其他問題,如不是隻聲明imageData,但沒有給出任何數據。

你也可以initWithContentsOfURL直接在一個NSImage中。

NSURL *imageURL = [NSURL URLWithString:@"http://i.stack.imgur.com/Zjno3.jpg?s=32&g=1"]; 
    NSImage * aImage = [[NSImage alloc] initWithContentsOfURL:imageURL]; 
    [_imageWellOutlet setImage: aImage]; //-- declared in .h header file 
              //-- (@property (weak) IBOutlet NSImageView *imageWellOutlet;) 
相關問題