私はiPhoneアプリケーションで忙しいです。その一部として、Flickr から 100 個の小さな画像を UIButton として読み込みます。問題はメモリをどう扱うかです。毎回約5〜6 mbをロードします。ビュートラフのナビゲーションコントローラ popViewController の割り当てを解除すると、メモリはほぼ同じままです。
私が今やっていることは、すべての画像をループして、要求を ASINetworkQueue に入れることです。ループの準備ができたら、[networkQueue go] を実行します。
//Loop images
for (NSDictionary* message in output)
{
NSString *imageURL = [[NSString alloc] initWithFormat:@"http://farm%@.static.flickr.com/%@/%@_%@_s.jpg", [message objectForKey:@"farm"], [message objectForKey:@"server"], [message objectForKey:@"id"], [message objectForKey:@"secret"]];
if(cx > 280) { cy = cy + 78; cx = 6.0f; }
//user data for the request
NSMutableDictionary *imageInfo = [[NSMutableDictionary alloc] init];
//init Image
CGRect myImageRect = CGRectMake(cx, cy, 72.0f, 72.0f);
UIButton *myImage = [[UIButton alloc] initWithFrame:myImageRect];
[myImage setTag:i];
[myImage setBackgroundColor:[UIColor whiteColor]]; //grayColor
[imageInfo setObject:[NSString stringWithFormat:@"%i", i] forKey:@"arrayNr"];
[images addObject: myImage];
[fotoView addSubview: myImage];
//get url
NSURL *imageURI = [[NSURL alloc] initWithString:imageURL];
//load image
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL: imageURI];
[request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:[message objectForKey:@"id"]]];
[request setUserInfo: imageInfo];
[networkQueue addOperation:request];
[imageURL release];
[imageURI release];
[myImage release];
[imageInfo release];
cx = cx + 78;
i++;
}
リクエストが終了したら、次のようにします。
- (void)imageFetchComplete:(ASIHTTPRequest *)request
{
UIImage *img = [[UIImage alloc] initWithContentsOfFile:[request downloadDestinationPath]];
if (img)
{
//set image
UIButton *myButton = [images objectAtIndex:[[[request userInfo] objectForKey:@"arrayNr"] intValue]];
[myButton setBackgroundImage:img forState: UIControlStateNormal];
[myButton addTarget:self action:@selector(showImage:) forControlEvents:UIControlEventTouchUpInside];
}
[img release];
}
そして、これは私の dealloc 関数です:
- (void)dealloc
{
[networkQueue cancelAllOperations];
networkQueue.delegate = nil;
[networkQueue release];
[flickrController cancelAllOperations];
flickrController.delegate = nil;
[flickrController release];
[images release];
[fotoView release];
[output release];
[super dealloc];
}
記憶が消えない理由がわかりません。私が考えることができる解決策は次のとおりです。 - uibutton/uiimage 用の独自のオブジェクトを作成します。- UITableview を使用して画像を処理する
どちらのソリューションも、ビューを閉じた後にすべてのメモリをクリアする方法がわかりません。この種の状況に対処する方法を誰かが教えてくれることを願っています。