我在 NSMutableArray 中放入了太多内容吗?请帮忙!
因此,我正在快速制作一个应用程序,尽管它不是最好的解决方案,但它似乎在大多数情况下都有效。所以我有一个简单的图像视图,可以从 NSMutableArray arr 中提取图像。我创建数组并以这种方式将其填充到 ViewDidLoad 内部:
arr = [[NSMutableArray alloc] init];
[arr addObject:[UIImage imageNamed:@"181940jpg"]];
[arr addObject:[UIImage imageNamed:@"168026.jpg"]];
[arr addObject:[UIImage imageNamed:@"168396.jpg"]];
[arr addObject:[UIImage imageNamed:@"168493_.jpg"]];
并且我继续对 130 个图像执行此操作。显然这是一个大阵。我不知道这是否是一个非常糟糕的方法,但如果是的话,我愿意接受建议!当我使用一些简单的后退和前进按钮浏览数组时,基于简单的计数器变量将图像从数组中拉出,一切正常,直到图像 45 左右。应用程序崩溃,控制台显示以下内容:
* ERROR: ImageIO 'ImageProviderCopyImageBlockSetCallback' header is not a CFDictionary...
如果我将图像分成单独的数组会有帮助吗?我是否在数组中放入了太多内容?我在这里错过了什么,相信我,我洗耳恭听。
谢谢
编辑:这里有一些更多信息
这就是我使用屏幕顶部设置的 UISegmentedControl 筛选数组的方式:
-(void) pickedOne{
if(segmentedControl.selectedSegmentIndex == 1){
NSLog(@"hey");
if(position < [arr count]-1){
position++;//This is my global counter variable
UIImage * img = [arr objectAtIndex:position];
[imageView setImage:img];
[img release];
}
}else if(segmentedControl.selectedSegmentIndex ==0){
if(position >0){
position--;
UIImage * img = [arr objectAtIndex:position];
[imageView setImage:img];
[img release];
}
}
}
这似乎不是内存管理的问题,但话又说回来,我不无论如何,我认为自己是这方面的专家......
So I am making an app really quick, and even though it isn't the best solution, it appears to be working for the most part. So I have a simple image view that pulls an image out of an NSMutableArray arr. I create the array and populate it inside of ViewDidLoad in this manner:
arr = [[NSMutableArray alloc] init];
[arr addObject:[UIImage imageNamed:@"181940jpg"]];
[arr addObject:[UIImage imageNamed:@"168026.jpg"]];
[arr addObject:[UIImage imageNamed:@"168396.jpg"]];
[arr addObject:[UIImage imageNamed:@"168493_.jpg"]];
ANd I continue doing that for 130 images. Obviously this is a big array. I don't know if this is like a really bad way to do it, but if it is, I am open to suggestions! As I go through the array with some simple back and forward buttons pulling images out of the array based on a simple counter variable things work ok until image 45-ish. The app breaks down and the console says this:
* ERROR: ImageIO 'ImageProviderCopyImageBlockSetCallback' header is not a CFDictionary...
Would it help if I broke my images up into separate arrays? Am I just putting too much into the array? What am I missing here, trust me, I am all ears.
Thanks
EDIT: Here is some more information
This is how I am sifting through the array, using a UISegmentedControl set on the top of the screen:
-(void) pickedOne{
if(segmentedControl.selectedSegmentIndex == 1){
NSLog(@"hey");
if(position < [arr count]-1){
position++;//This is my global counter variable
UIImage * img = [arr objectAtIndex:position];
[imageView setImage:img];
[img release];
}
}else if(segmentedControl.selectedSegmentIndex ==0){
if(position >0){
position--;
UIImage * img = [arr objectAtIndex:position];
[imageView setImage:img];
[img release];
}
}
}
It doesn't appear to be a problem with memory management, but then again, I don't consider myself a pro at that by any means...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就可变数组而言,您放入其中的只是指向其他对象的指针。您必须担心的是那些其他对象,是的,您拥有太多(很可能)。
这听起来更像是您遇到了过度释放问题,并且正在向 ImageIO API 传递一些虚假内容。
事实上,这正是您所拥有的:
那个
release
是虚假的;它不会平衡代码中任何地方的保留。objectAtIndex:
不会返回保留的对象,UIView
将负责在内部保留/释放图像。删除该
版本
(还有另一个)!您仍然需要担心内存消耗。每张图像的大小为 42K(并非不合理的大小,而是完全虚构的),在作为存储的一部分进行任何解压缩或其他扩展之前,130 个图像的重量约为 6MB 左右。
这些设备的内存非常有限。
In terms of the mutable array, what you put into it is just a pointer to some other object. It is those other objects you have to worry about and, yes, you have too many of them (more likely than not).
That sounds more like you have an over-release problem and are passing something bogus to the ImageIO APIs.
And, in fact, that is exactly what you have:
That
release
is spurious; it does not balance a retain anywhere in your code.objectAtIndex:
does not return a retained object and theUIView
will take care of retaining/releasing the image internally.Remove that
release
(and the other one, too)!You still need to worry about memory consumption. At a size of 42K each (not an unreasonable size, but entirely made up), 130 images will weigh in at ~6MB or so, prior to any decompression or other expansion that occurs as a part of storage.
The devices are quite memory constrained.