2013-03-19 106 views
0

我需要在UICollectionView上显示大约300个图像。我添加了自定义的UICollectionViewCell和子视图UIImageView,现在当我尝试在Web服务器上显示UICollectionView上的图像时,它显示大约150个图像,然后显示低级内存的应用程序崩溃。下面是我的代码,请让我知道我做错了什么。UICollectionView应用程序崩溃在iPhone上显示大量图像

-(void)updateView { 

    for (int i = 0; i < m_nPhotoCount; i ++) 
    { 
     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 

      NSURL *imageURL = [NSURL URLWithString:[aryList objectAtIndex:i]]; 
      UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]]; 
      if(image == nil) 
      { 
       NSLog(@"kashif"); 
      } 
      else { 
       [self.imageArray addObject:image]; 
      } 
      dispatch_sync(dispatch_get_main_queue(), ^{ 
       [collectionView reloadData]; 
      }); 
     }); 
    } 
} 

    #pragma mark - UICollectionView Datasource 

    - (NSInteger)collectionView:(UICollectionView *)view numberOfItemsInSection:(NSInteger)section { 

     return [self.imageArray count]; 
    } 


    - (UICollectionViewCell *)collectionView:(UICollectionView *)cv cellForItemAtIndexPath:(NSIndexPath *)indexPath { 

     static NSString *CellIdentifier = @"GalleryCell"; 
     GalleryCell *cell = (GalleryCell*)[cv dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath]; 
     cell.imageView.image = [self.imageArray objectAtIndex:indexPath.row]; 
     return cell; 
    } 

    - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath { 

     return [[UICollectionReusableView alloc] init]; 

    } 

回答

1

你正在采取错误的做法。你应该做的是,你应该只下载一次这些数量的图像(相当于CollectionView的可见单元的数量)。

当收集视图滚动时,比下载更多图像和清除或将先前图像清除或存储在缓存中。

你可以利用这个美好的图片下载和缓存库:SDWebImage

+0

您好,以前我已经尝试过使用UITableView的SDWebImage它没有工作,请你帮我用UICollectionView进行缓存。 – user1237473 2013-03-19 05:58:48

+0

@ user1237473你的意思是“It Did not Work”,你在你的viewController中导入了UITableView特定的图像缓存头文件,用于缓存Tableview中的图像? – 2013-03-19 06:08:02

-1

有可供馆专题库。您应该尝试使用库来处理或创建大尺寸的集合视图。