我试图从URL中显示一些图像到我的集合视图。但是当我尝试这样做时,我的图像显示在集合视图上,但它们随机保持刷新和重新加载。另外当我尝试滚动collectionView,它再次重新加载整个数据,我不想要。我只想将图像显示为预览给用户。 下面是我的代码,从URL获取图像并将其显示在集合视图中。如何停止在CollectionView中自动重新加载和刷新图像iOS
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"cellIdentifier" forIndexPath:indexPath];
__block NSString *filename;
for(NSDictionary *item in [jsonSD objectForKey:@"Data"]) {
for (int i=0; i<=[[item objectForKey:@"Count"]integerValue]; i++) {
filename=[NSString stringWithFormat:@"http://..URL../media/content/%@%@%d_1.png",_getstring,[item objectForKey:@"subcategory_id"],i];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = [NSURL URLWithString:filename];
[self downloadImageWithURL:url completionBlock:^(BOOL succeeded, NSData *data) {
if (succeeded) {
iv.image = [[UIImage alloc] initWithData:data];
}
}];
});
}
}
return cell;
}
- (void)downloadImageWithURL:(NSURL *)url completionBlock:(void (^)(BOOL succeeded, NSData *data))completionBlock
{
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (!error) {
completionBlock(YES, data);
} else {
completionBlock(NO, nil);
}
}];
}
这里'iv'是我的imageView,其中我显示的图像收集视图的单元格。 我试过搜索相关内容,但对上述问题我没有任何想法。
请帮帮我。任何帮助表示赞赏。
你发现它是正确的。我不想在一个单元格中显示所有图像。我想单个单元格的单个图像。 – ammy1
你能帮我吗。 – ammy1
@ Greg-感谢您的建议。我会试试看。 – ammy1