【发布时间】:2014-03-31 21:08:17
【问题描述】:
我正在开发一个带有UICollectionView 和ViewController 的应用程序。
这个CollectionView 是一个画廊,每行有 3 张图片。
我以 30 张图片为一组从服务器获取图像,第一个 url 和代码,当单元格将显示在 cellForItemAtIndexPath 时,我使用 SDWebImage Library 异步下载这些图片。每 30 张图片我再次调用 Web 服务,我会收到 30 张图片,发送带有限制和偏移的请求。这可能发生 100 次,有 3000 张图片的配置文件。
好吧,我的问题来了,我在 iPhone 4 中启动了该应用程序,经过快速滚动后,我开始收到 Received Memory Warnings,并在收到一些警告后应用程序崩溃了。当我在模拟器中进行相同的测试时,没有任何不好的事情发生。
每次下载 30 张图片时,我都会将结果数组添加到处理 CollectionView 数据的 NSMutableArray *data property 和 reload collectionView。我曾尝试将 Instruments 与分配一起使用,但我很难理解发生了什么。
这是我用来创建单元格的代码
-(UICollectionViewCell *)collectionView:(LZProfileImagesCollectionView *)aCollectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
if(aCollectionView == self.imagesCollectionView){
if([self.data count] - 12 == indexPath.row){
self.photoOffset += 30;
[self loadUserData];
}
LZProfileCollectionViewCell * cell = (LZProfileCollectionViewCell *)[imagesCollectionView dequeueReusableCellWithReuseIdentifier:ImageCollectionCellIdentifier forIndexPath:indexPath];
Image *image =(self.data)[indexPath.row];
[cell configureCellWithImage:image];
return cell;
}
return nil;
}
在LZProfileCollectionViewCell 单元格中,我有这个:
-(void)configureCellWithImage:(Image *)image
{
[self setNeedsDisplay];
// Imagen
NSString *imageUrl = [kBaseURL stringByAppendingString:image.imageStringUrl];
[self.pictureImg setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:kDefaultLoadingImage]];
[self.pictureImg setContentMode:UIViewContentModeScaleAspectFit];
}
我已从SDWebImage 取走setImageWithURL
3 分钟后,我在 Instruments(Statistics) 中获得了这张快照
提前谢谢你
【问题讨论】:
-
如果您使用的是第三方库,您需要确保已对其进行配置以进行适当的缓存。当您需要释放内存时,您还负责清除该缓存。分配工具对内存的使用位置有何看法?
-
我使用
AFNetworking处理普通请求,SDWebImage用于图像异步下载。您希望在工具分配中看到什么?调用树,分配列表? -
从
Statistics->Object Summary开始,然后查看顶部的Live Bytes对象。或发布屏幕截图。也拍一些照片。
标签: ios iphone objective-c ios7 uicollectionview