在UIScrollView中加载200多个子视图图像时程序崩溃 [英] Program crashes when loading 200+ subview images in the UIScrollView

查看:78
本文介绍了在UIScrollView中加载200多个子视图图像时程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ALAssetLibrary在iPhone中开发像Photos这样的类似程序。我试图在scrollview中加载图像。当专辑有少量图片时,一切正常。但是当我尝试用200多张照片加载相册时,我的程序结束时没有任何错误消息。有人知道这个节目吗?
这是我加载滚动视图的代码:

I am developing a similar program like Photos in the iPhone using ALAssetLibrary. I am trying to load the images in a scrollview. Everything works fine when the album has small amount of pictures. But when I trying to load the album with 200+ photos, my program ended without any error message. Anyone know this program? Here is my code for loading scroll view:

- (void)loadScrollView
{
for (UIView *v in [scrollview subviews]) {
    [v removeFromSuperview];
}

CGRect scrollFrame = [self frameForPagingScrollView];
scrollview = [[UIScrollView alloc] initWithFrame:scrollFrame];

CGRect workingFrame = scrollview.frame;
workingFrame.origin.y = 0;

photoCount = [info count];
for(NSDictionary *dict in info) {
    UIImageView *imageview = [[UIImageView alloc] initWithImage:[dict objectForKey:UIImagePickerControllerOriginalImage]];
    [imageview setContentMode:UIViewContentModeScaleAspectFit];
    imageview.frame = workingFrame;

    [scrollview addSubview:imageview];
    [imageview release];
    [scrollview setPagingEnabled:YES];
    [scrollview setDelegate:self];
    [scrollview setAutoresizesSubviews:YES];
    [scrollview setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
    [scrollview setShowsVerticalScrollIndicator:NO];
    [scrollview setShowsHorizontalScrollIndicator:NO];

    workingFrame.origin.x = workingFrame.origin.x + workingFrame.size.width;

}

[self setScrollViewContentSize];
[[self view] addSubview:scrollview];
}

非常感谢提前!!!

推荐答案

我个人将所有 UIImageView 对象放在我的 UIScrollView ,但仅为当前可见的那些设置它们的图像属性(并清除那些不再可见的图像属性)。如果你有成千上万的图像,甚至可能太浪费了(也许你甚至不想保留 UIImageView 对象,即使没有它们的图像属性设置,周围),但如果你正在处理数百个,我发现这是一个很好的简单解决方案,解决了 UIImage所消耗的内存的关键问题对象:

I personally put all of my UIImageView objects on my UIScrollView, but only set the image property for them for those that are currently visible (and clear the image property for those that are no longer visible). If you have thousands of images, perhaps even that is too wasteful (perhaps you don't even want to keep the UIImageView objects, even without their image property set, around), but if you're dealing with hundreds, I find it is a nice easy solution, addressing the key problem of the memory consumed by the UIImage objects:

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.scrollView.delegate = self;

    // all of my other viewDidLoad stuff...
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [self loadVisibleImages];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self loadVisibleImages];
}

- (void)loadVisibleImages
{
    CGPoint contentOffset = self.scrollView.contentOffset;
    CGRect  contentFrame  = self.scrollView.bounds;
    contentFrame.origin = contentOffset;

    for (UIImageView *imageView in _imageViews) // _imageViews is (obviously) my array of images 
    {
        if (CGRectIntersectsRect(contentFrame, imageView.frame))
        {
            imageView.image = ... // set the image property
        }
        else
        {
            imageView.image = nil;
        }
    }
}

这是一些来自某些人的片段代码正在做其他一些事情,所以很明显你的实现会有很大不同,但它显示了如何使用 scrollViewDidScroll 来确定哪些图像可见并适当加载/卸载图像。如果你想删除/添加 UIImageView 对象,你可能会进一步改变,但显然必须改变这个imageview可见的逻辑,而不是而不是利用所有 UIImageView 对象的框架

This is a snippet from some code that's doing a bunch of other stuff, so clearly your implementation will differ significantly, but it shows how you can use scrollViewDidScroll to determine what images are visible and load/unload images appropriately. You could probably alter further if you want to also remove/add the UIImageView objects, too, but clearly the logic of "is this imageview visible" would have to be changed, rather than leveraging the frame of all of the UIImageView objects.

我不确定我是否正确阅读你的代码,但你是否也将所有 UIImage 对象都放在字典中?这本身就非常奢侈地使用了内存。我通常将实际图像保存在某个持久存储中(例如,我使用 Documents 文件夹,但您可以使用核心数据 SQLite ,尽管后两者对大型图像造成了显着的性能影响)。我保留在内存中的唯一图像是UI主动使用的图像,我将使用 NSCache 对象来保持一些性能原因,但我否则将它们拉出来来自持久存储,而非活动内存。

I'm not sure if I'm reading your code right, but do you also have all of your UIImage objects sitting in a dictionary, too? That's pretty extravagant use of memory, itself. I usually keep the actual images in some persistent store (e.g. I use Documents folder, though you could use Core Data or SQLite, though the latter two impose a significant performance hit for large images). The only images I keep in memory are the ones actively used by the UI and I'll use a NSCache object to keep a few around for performance reasons, but I otherwise pull them from persistent storage, not active memory.

这篇关于在UIScrollView中加载200多个子视图图像时程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆