2009-07-25 28 views
0

我從網上下載.jpg文件與此代碼:如何將NSMutableData轉換爲視圖中的圖像?

NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://cvcl.mit.edu/hybrid/cat2.jpg"]]; 
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; 
receivedData=[[NSMutableData data] retain]; // etc etc 

,一切工作正常。我只是不知道如何處理數據。假設我在IB創建了一個圖像視圖,我將如何去顯示圖像?下面是最後一位:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    // ******do something with the data*********...but how 
    NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]); 

    // release the connection, and the data object 
    [connection release]; 
    [receivedData release]; 
} 

回答

1

的UIImage有一個方法+ imageWithData:

+0

如果你在Mac上需要這個,使用NSImage的initWithData:方法。 – 2009-07-25 00:58:06

2

假設你NSImageView附於所謂imageView,你應該能夠使用接收到的數據初始化圖像的IBOutlet,然後設置圖像查看以顯示圖像。

NSImage *image = [[NSImage alloc] initWithData:receivedData]; 
[imageView setImage:image]; 
[image release];