0

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];
}

Thanks a lot in advance!!!

4

3 回答 3

1

我个人将所有UIImageView对象都放在 my 上UIScrollView,但仅为当前可见的对象设置图像属性(并为不再可见的对象清除图像属性)。如果您有数千张图像,那可能也太浪费了(也许您甚至不想保留这些UIImageView对象,即使没有image设置它们的属性),但是如果您要处理数百张图像,我觉得这是一个不错的选择简单的解决方案,解决对象消耗内存的关键问题UIImage

- (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对象,您可能还可以进一步更改,但显然必须更改“此图像视图是否可见”的逻辑,而不是利用frame所有UIImageView对象。

我不确定我是否正确阅读了您的代码,但是您是否也将所有UIImage对象都放在字典中?这本身就是对内存的过度使用。我通常将实际图像保存在一些持久性存储中(例如,我使用Documents文件夹,尽管您可以使用Core Dataor SQLite,尽管后两者对大图像造成显着的性能影响)。我保留在内存中的唯一图像是 UI 积极使用的图像,NSCache出于性能原因,我将使用一个对象来保留一些图像,但否则我会从持久存储中提取它们,而不是从活动内存中提取它们。

于 2012-08-09T17:08:19.517 回答
0

您应该使用滚动视图委托来确定当前将在屏幕上显示哪些图像,并且仅将那些图像加载到内存中。

此外,如果您显示的图像尺寸远小于实际图像尺寸,则应调整图像大小并使用较小的图像。

于 2012-08-09T16:19:15.057 回答
0

为什么不使用UICollectionViewUICollectionViewController类? UICollectionViewController参考这里

示例代码在这里

您的图像最终将成为UICollectionViewCell. 数据源和委托协议方法类似于 UITableView 方法。即它们提供了一种机制,UICollectionViewController将重用UICollectionViewCells.

使用这些类的好处是它们的使用方式类似UITableViewControllers,它们可以帮助您解决您遇到的内存压力问题。

祝你好运!

于 2013-05-10T15:21:09.760 回答