0

次のコードを使用して、インターネットから .jpg ファイルをダウンロードしました。

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];
}
4

2 に答える 2

2

NSImageViewが着信側として接続されているIBOutletと仮定するとimageView、受信したデータを使用して画像を初期化し、画像を表示するように画像ビューを設定できるはずです。

NSImage *image = [[NSImage alloc] initWithData:receivedData];
[imageView setImage:image];
[image release];
于 2009-07-25T00:59:43.840 に答える
1

UIImageにはメソッド+imageWithDataがあります。

于 2009-07-25T00:56:11.687 に答える