我忙於使用iPhone應用程序。它的一部分,我從Flickr載入100個小圖像作爲UIButton。 問題是如何處理內存。它每次加載大約5-6 mb。當我通過navigationController popViewController釋放視圖槽時,內存保持幾乎相同。加載100 UIImages
我現在所做的是循環所有圖像,並將請求放入ASINetworkQueue。當循環準備就緒時,我會[networkQueue去]。
//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處理圖像
這兩種解決方案我都沒有看到在關閉視圖後它將如何清除所有內存。 希望有人給我一個提醒如何處理這種情況。
我把其餘的代碼放在我的第一篇文章中。我做的嘗試是這樣的 - (void)dealloc但它失敗了。 \t爲(在圖像的UIButton *按鈕) \t { \t \t [[按鈕backgroundImageForState:UIControlStateNormal]釋放]; \t \t [button removeFromSuperview]; \t \t [button release]; \t} – 2010-07-12 11:55:55
是否調用了dealloc方法? – 2010-07-12 13:00:25
我就像你問這個問題的原因。 dealloc被調用是正常的。但它沒有被調用。所以我現在解決了這個問題。希望能爲它找到解決方案 – 2010-07-14 09:51:59