2009-07-25 8 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:メソッドを使用してください。 –

+0

よろしくお願いします。それはMacの質問ですか?私はあまりにも長くプログラミングしています:) – mjhoy

+0

それはMacかiPhoneの質問かどうかの兆候はありません。 –

2

と仮定すると、あなたのNSImageViewあなたが受信したデータを使用して画像を初期化することができるはずimageView呼ばIBOutletとして添付して、画像を設定されています画像を表示するためのビュー。

NSImage *image = [[NSImage alloc] initWithData:receivedData]; 
[imageView setImage:image]; 
[image release]; 
関連する問題