加载 100 个 UIImages
我正忙于开发 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 来处理图像
这两种解决方案我不知道关闭视图后如何清除所有内存。 希望有人提醒我如何处理这种情况。
I am busy with an iPhone application. A part of it I load 100 small images from Flickr as an UIButton.
The problem is how to handle the memory. it loads around 5-6 mb each time. When I dealloc the view trough navigationController popViewController the memory stays almost the same.
What I do now is loop all the images and put the request to an ASINetworkQueue. When the loop is ready I do [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++;
}
When the request is finished I do this:
- (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];
}
And this is my dealloc function:
- (void)dealloc
{
[networkQueue cancelAllOperations];
networkQueue.delegate = nil;
[networkQueue release];
[flickrController cancelAllOperations];
flickrController.delegate = nil;
[flickrController release];
[images release];
[fotoView release];
[output release];
[super dealloc];
}
I don't understand way the memory doesn't go away.
Solution what i can think about are:
- create an own object for the uibutton/uiimage.
- Use an UITableview to handle the images
Both solution I don't see how it would clear all the memory after closing the view.
Hope someone give me a heads-up how to handle this kind of situations.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您发布了进行清理的代码,可能会更清楚问题出在哪里,但我还是在猜测:
在
imageFetchComplete:
方法中,当您因此,调用setBackgroundImage:
它将保留在内存中,直到您释放保留它的特定按钮对象。由于按钮本身由images
数组保留,因此您也需要释放该数组,或者在可变数组的情况下从数组中删除按钮。If you posted the code where you do the cleanup, it might be clearer where the problem is, but I'm taking a guess anyway:
In the
imageFetchComplete:
method you retain the reference to the image when you invokesetBackgroundImage:
It will therefore be kept in memory until you release the particular button object that retained it. Since the button itself is being retained by theimages
array, you would either need to release this array too, or in case of a mutable array remove the button from the array.