Ios 异步下载1000个图像并进行缓存,但内存不足
我有一个代码可以下载大约1000幅图像,还可以使用Ios 异步下载1000个图像并进行缓存,但内存不足,ios,objective-c,uicollectionview,Ios,Objective C,Uicollectionview,我有一个代码可以下载大约1000幅图像,还可以使用NSCache执行缓存,并在UICollectionView中显示图像。它可以工作,但当UICollectionView滚动非常快且internet运行缓慢时,会收到内存警告。那么这里的问题是什么呢?下面是我的代码:提前谢谢 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath
NSCache
执行缓存,并在UICollectionView
中显示图像。它可以工作,但当UICollectionView滚动非常快且internet运行缓慢时,会收到内存警告。那么这里的问题是什么呢?下面是我的代码:提前谢谢
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
ProductCollectionViewCell *cell = (ProductCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
//set image,title of product
cell.productLabel.text = [self.productNameArray objectAtIndex:indexPath.row];
UIImage *image = [self.imgCache getCachedImageForKey:[self.imgUrlArray objectAtIndex:indexPath.row]];
if (image.size.width>0) {
[cell.productImage setImage:image];
}
else {
[cell.productImage setImage:nil];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[self.imgUrlArray objectAtIndex:indexPath.row]]];
UIImage *img = [UIImage imageWithData:imgData];
[cell.productImage setImage:nil];
dispatch_async(dispatch_get_main_queue(), ^{
if (imgData!=nil) {
[cell.productImage setImage:img];
[self.imgCache cacheImage:img forKey:[self.imgUrlArray objectAtIndex:indexPath.row]];
if ([self.imgDataArray count] <= [self.productNameArray count]) {
[self.imgDataArray addObject:imgData];
}
}
});
if ([self.imgDataArray count] == [self.productNameArray count]){
[self.productsView.collectionView reloadData];
}
});
}
return cell;
}
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath{
ProductCollectionViewCell*单元格=(ProductCollectionViewCell*)[collectionView出列ReusableCellWithReuseIdentifier:indexPath的标识符:indexPath];
//设置产品的图像、标题
cell.productLabel.text=[self.productNameArray objectAtIndex:indexath.row];
UIImage*image=[self.imgCache getCachedImageForKey:[self.imgUrlArray objectAtIndex:indexPath.row]];
如果(图像大小宽度>0){
[cell.productImage setImage:image];
}
否则{
[cell.productImage setImage:nil];
调度异步(调度获取全局队列(调度队列优先级高,0),^(无效){
NSData*imgData=[NSData dataWithContentsOfURL:[NSURL URLWithString:[self.imgUrlArray objectAtIndex:indexPath.row]];
UIImage*img=[UIImage-imageWithData:imgData];
[cell.productImage setImage:nil];
dispatch\u async(dispatch\u get\u main\u queue()^{
如果(imgData!=nil){
[cell.productImage setImage:img];
[self.imgCache cacheImage:img forKey:[self.imgUrlArray objectAtIndex:indexath.row]];
如果([self.imgDataArray count]使用SDWebImage
framework。
以下是链接或如何将其与项目链接:
下面是如何使用SDWebImage
下载图像的代码行
[cell.itemimage sd_setImageWithURL:[NSURL URLWithString:URLString] placeholderImage:[UIImage imageNamed:@"loadingImage"] options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
cell.itemimage.image = image;
dispatch_async(dispatch_get_main_queue(), ^{
[cell setNeedsDisplay];
});
}];
使用SDWebImage
framework。
以下是链接或如何将其与项目链接:
下面是如何使用SDWebImage
下载图像的代码行
[cell.itemimage sd_setImageWithURL:[NSURL URLWithString:URLString] placeholderImage:[UIImage imageNamed:@"loadingImage"] options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
cell.itemimage.image = image;
dispatch_async(dispatch_get_main_queue(), ^{
[cell setNeedsDisplay];
});
}];
你可以用这个
你可以用这个
尝试使UIImageView类别文件用于从url下载图像,它会将该文件保存在设备的缓存中,并且当您想再次加载该图像时,它会从缓存中加载该图像。以下是我的代码链接,用于此尝试使UIImageView类别文件用于从url下载图像,它会将文件保存在设备的缓存中,当您想再次加载该图像时,它将从缓存中加载该图像。以下是我的代码链接,用于从url获取数据并在cellForItemAtIndexPath内调用imageWithData似乎不太好。虽然图像未缓存,但它将尝试每次获取数据调用单元格的时间,这不是建议的方法。在外部函数中获取图像数据,然后仅重新加载表视图或相应的行
试试这样的
- (void)downloadImages {
for (int i = 0; i < self.imgUrlArray.count; i++) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[self.imgUrlArray objectAtIndex:i]]];
if(imgData) {
UIImage *img = [UIImage imageWithData:imgData];
if(img) {
// Cache image
dispatch_async(dispatch_get_main_queue(), ^{
// reload collection view
});
}
}
});
}
}
从url获取数据并在cellForItemAtIndexPath内调用imageWithData似乎不太好。虽然未缓存图像,但每次调用单元格时它都会尝试获取数据,这不是建议的方法。在外部函数中获取图像数据,然后仅重新加载表视图或相应的行
试试这样的
- (void)downloadImages {
for (int i = 0; i < self.imgUrlArray.count; i++) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[self.imgUrlArray objectAtIndex:i]]];
if(imgData) {
UIImage *img = [UIImage imageWithData:imgData];
if(img) {
// Cache image
dispatch_async(dispatch_get_main_queue(), ^{
// reload collection view
});
}
}
});
}
}