我正忙于一个 iPhone 应用程序。其中一部分我从 Flickr 加载 100 个小图像作为 UIButton。问题是如何处理内存。每次加载大约 5-6 mb。当我通过 navigationController 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 处理图像
这两种解决方案我都看不到在关闭视图后它会如何清除所有内存。希望有人提醒我如何处理这种情况。