gpt4 book ai didi

iphone - Objective-c UIImage 存储在 MSMutableArray 替代方案中,潜在的内存泄漏?

转载 作者:行者123 更新时间:2023-11-29 13:37:44 24 4
gpt4 key购买 nike

我正在使用 ARC 构建适用于 iOS 5 的应用程序,我似乎遇到了一些内存问题。基本上它拍摄了一部分显示的屏幕截图,将 UIImage 放在 MSMutableArray 中,然后将屏幕截图拼凑成一个大图像。现在的问题是,在执行了几次之后,由于内存使用率过高,操作系统关闭了应用程序。

这是将 UIImage 拼凑在一起的片段:

UIImage* finalImage = nil;
//join the screenshot images together
UIGraphicsBeginImageContext(CGSizeMake(collage.width, collage.height));
{
int hc = 0;
for(UIImage *img in imageArr)
{
NSLog(@"drawing image at:: %i", hc);
[img drawAtPoint:CGPointMake(0, hc)];
hc+=img.size.height;
img = nil;
}

//NSLog(@"creating finalImage");
finalImage = UIGraphicsGetImageFromCurrentImageContext();
}
UIGraphicsEndImageContext();
//do something with the combined image
//remove all the objects
[imageArr removeAllObjects];
//reset class instance
[self setImageArr: [[NSMutableArray alloc] init]];

我可以使用它们来避免占用太多内存吗?也许在数组中存储 CGImageRef?上述代码是否存在潜在的内存泄漏?

任何提示,指针将不胜感激。

谢谢。

最佳答案

[imageArr removeAllObjects]; 将从数组中删除对象。无需使用

再次重置数组
 [self setImageArr: [[NSMutableArray alloc] init]];

这样做是在分配一个 NSMutableArray 对象而不是释放它。

尝试删除行 [self setImageArr: [[NSMutableArray alloc] init]];

关于iphone - Objective-c UIImage 存储在 MSMutableArray 替代方案中,潜在的内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10047594/

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