简体   繁体   中英

UIScrollView Lazy Loading Images

My app for the iPhone Contains 170 images. I read them into an array, and in the loop add a picture in the viewfinder, then put the image View as sub view of scroll view.

When running my app uses too much memory 700mb. I tried to reduce the size of the pictures, but it didn't work.

One of my friends said that I should add only images # 1 and # 2. When the user block is scrolled to the picture No. 1, then only show the picture No. 2. Then the picture No. 1 to remove from the image viewer and add the picture No. 3.

He says that in this way I can maintain the normal memory consumption. But I don't understand how to do this?

Could you help with an example? Thanks in advance.

Not using UICollectionView

Here is my code:

- (void)addImageViewToScrollView {
    _assetsArray = [[NSMutableArray alloc] init];
    for (int j = 0; j < 170; j++) {
        [_assetsArray addObject:[UIImage imageNamed:[NSString stringWithFormat:@"%d",j]]];
    }

    //scrollView add subview ImageView
    for (int i = 0; i < [_assetsArray count]; i++) {
        CGRect frame;
        frame.origin.x = self.scrollView.frame.size.width *i;
        frame.origin.y = 0;
        frame.size = self.scrollView.frame.size;
        _imageView = [[UIImageView alloc]init];
        _imageView.image = [_assetsArray objectAtIndex:i];
        _imageView.frame = frame;
        [self.scrollView addSubview:_imageView];
    }
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * self.assetsArray.count, self.scrollView.frame.size.height);
}

--

在此处输入图片说明

Use a UICollectionViewController to solve this issue.

This way only the cells that are required on screen are loaded and all others images are can be popped from cache when you hit a memory warning.

Collection Views work very similar to table views.

To get the collection view to suit what you're doing you'll need to set the flow layout to horizontal scrolling and set the cell sizes to your view's height and width.

Seems like you are trying to add 170 images onto UIScrollView using UIImageView. Why not use dedicated UITableView's or UICollectionView's ? Object of Reusable Cell is to maintain run-time memory consumption. Let's recap on this,

  1. You have 170 ImageViews ( _assetsArray ) ; expensive consumption
  2. You have 170 Images

Versus, Using UITableView or UICollectionView

  1. You have 1 ImageView in a Cell with 170 rows
  2. You have DataSource of 170 Images were loaded
  3. Your cell will be re-cycled when it needed to display correspond to DataSource

Also, for supporting the multitude images, there are good libraries to reduce images cache. Try to use SDWebImage or FastImageCache . I personally like FIC but you might need to understand the concept.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM