gpt4 book ai didi

iphone - 加载 100 个 UIImages

转载 作者:行者123 更新时间:2023-12-03 21:11:13 25 4
gpt4 key购买 nike

我正忙于处理 iPhone 应用程序。其中一部分是我从 Flickr 加载 100 个小图像作为 UIButton。问题是如何处理内存。每次加载大约 5-6 MB。当我通过 navigationController popViewController 释放 View 时,内存几乎保持不变。

我现在所做的是循环所有图像并将请求放入 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 处理图像

这两种解决方案我都不知道关闭 View 后如何清除所有内存。希望有人提醒我如何处理这种情况。

最佳答案

如果您在进行清理的地方发布了代码,可能会更清楚问题出在哪里,但无论如何我都会猜测:

imageFetchComplete: 方法中,当您调用 setBackgroundImage: 时,您会保留对图像的引用,因此它将保留在内存中,直到您释放保留的特定按钮对象。它。由于按钮本身由 images 数组保留,因此您也需要释放该数组,或者在可变数组的情况下从数组中删除按钮。

关于iphone - 加载 100 个 UIImages,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3227592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com