Ios UICollectionViewCell中的异步图像下载?
我正在使我的UIcollectionviewcell异步下载图像,但最后一个单元格只有最后一个图像。下面是我的代码Ios UICollectionViewCell中的异步图像下载?,ios,iphone,Ios,Iphone,我正在使我的UIcollectionviewcell异步下载图像,但最后一个单元格只有最后一个图像。下面是我的代码 - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { self.collectionViewCell = [collectionView dequeueReusableCellWi
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
self.collectionViewCell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
NSMutableDictionary *temperoryDictionary = [[NSMutableDictionary alloc] initWithDictionary:[self.weatherArray objectAtIndex:indexPath.row]];
NSString *imageURL = [[[temperoryDictionary valueForKey:@"weatherIconUrl"] objectAtIndex:0] valueForKey:@"value"];
[self downloadImageWithURL:[NSURL URLWithString:imageURL] completionBlock:^(BOOL succeeded, UIImage *image) {
if (succeeded) {
self.collectionViewCell.weatherImage.image= image;
}
}];
return self.collectionViewCell;
}
- (void)downloadImageWithURL:(NSURL *)url completionBlock:(void (^)(BOOL succeeded, UIImage *image))completionBlock{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (!error)
{
UIImage *imageAsync = [[UIImage alloc] initWithData:data];
completionBlock(YES,imageAsync);
} else {
completionBlock(NO,nil);
}
}];
}
我不知道我犯了什么错误。您应该使用
块UICollectionViewCell*单元格,而不是self.collectionViewCell
:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
__block UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
NSMutableDictionary *temperoryDictionary = [[NSMutableDictionary alloc] initWithDictionary:[self.weatherArray objectAtIndex:indexPath.row]];
NSString *imageURL = [[[temperoryDictionary valueForKey:@"weatherIconUrl"] objectAtIndex:0] valueForKey:@"value"];
[self downloadImageWithURL:[NSURL URLWithString:imageURL] completionBlock:^(BOOL succeeded, UIImage *image) {
if (succeeded) {
cell.weatherImage.image= image;
}
}];
return cell;
}
对于自定义单元格,请使用\u块CustomClass*单元格
CustomClass
是自定义单元格的类别。self.collectionViewCell
将仅引用1个单元格。将其替换为局部变量UICollectionViewCell*collectionViewCell
您不需要最后一个单元格的指针,因为它很可能是图像加载完成后tableView要求的最后一个单元格
相反,请尝试以下方法:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
NSInteger currentTag = indexPath.row;
cell.tag = currentTag;
NSMutableDictionary *temperoryDictionary = [[NSMutableDictionary alloc] initWithDictionary:[self.weatherArray objectAtIndex:indexPath.row]];
NSString *imageURL = [[[temperoryDictionary valueForKey:@"weatherIconUrl"] objectAtIndex:0] valueForKey:@"value"];
__weak __typeof(cell) weakCell = cell;
[self downloadImageWithURL:[NSURL URLWithString:imageURL] completionBlock:^(BOOL succeeded, UIImage *image) {
if (succeeded && weakCell.tag == currentTag) {
weakCell.weatherImage.image = image;
}
}];
}
这将保留一个指向当前单元格的弱指针,加载完图像后,它将检查单元格的标记(indexPath.row)是否相同,并在这种情况下设置图像。然后如何获取自定义UICollectionViewCell的数据源只需返回collectionViewCell
局部变量即可。您不需要成员变量。@anhtu提到的答案应该解决返回单元格的问题代码>不返回self.collectionViewCell代码>我正在使用自定义单元格。。。使用图像视图。如果我能用你的解决方案。。我的自定义单元格属性将如何获取我的图像…@AnujVats我编辑了我的答案。请试一试。谢谢,我建议在设置图像之前检查单元格的实际索引,因为在单元格被滚动到屏幕外并重新用于另一个索引路径后,加载可能会完成。我的答案不是最好的(正如@mbo42所提到的)。但它可以解决你的问题。对于您的代码,假设您有2个单元格self.collectionViewCell
指向单元格1并调用downloadImageWithURL
。然后,self.collectionViewCell
指向单元格2。但是下载带有URL的ImageWithURL
尚未完成。所以当下载带有URL的图像时
完成。单元格2获取单元格1的图像(因为self.collectionViewCell
现在指向单元格2)。所以1号牢房没有任何图像。呃。。。它为加载块保留一个弱本地指针,而不是ViewController。。。ViewController根本不需要保留指向单元格的指针