Ios 下载单元格内容仅用于可见
我有Ios 下载单元格内容仅用于可见,ios,objective-c,uicollectionview,uicollectionviewcell,sdwebimage,Ios,Objective C,Uicollectionview,Uicollectionviewcell,Sdwebimage,我有UICollectionViewCell和动态内容下载(图像下载)。我已在手机中以块形式下载: -(MainVCCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"MainVCCell"; MainVCCell *cell = [collecti
UICollectionViewCell
和动态内容下载(图像下载)。我已在手机中以块形式下载:
-(MainVCCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"MainVCCell";
MainVCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
Person *person = [self.fetchedResult objectAtIndex:indexPath.row];
[cell.login setText:person.login];
if(person.avatar) {
[cell.avatarImageView setImage:[UIImage imageWithData:person.avatar]];
} else {
[cell.avatarImageView setImage:[UIImage imageNamed:@"placeholder"]];
[AsyncUrl request:[NSString stringWithFormat:@"some SSL URL",person.login] completeBlock:^(NSData *data) {
dispatch_queue_t downloadQueue = dispatch_queue_create("Download queue", NULL);
dispatch_async(downloadQueue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
MainVCCell *cellToUpdate = (MainVCCell*)[collectionView cellForItemAtIndexPath:indexPath];
if(cellToUpdate) {
[cellToUpdate.avatarImageView setImage:[UIImage imageWithData:data]];
}
person.avatar = data;
[[CoreDataController sharedInstance] saveContext];
});
});
} errorBlock:^(NSError *error) {
NSLog(@"%@",error);
}];
}
return cell;
}
它工作得很好,但当然,当我滚动几次时,我会得到很多连接并下载fire,其中一些甚至会超时。我明白为什么会这样。有没有办法取消不可见单元块中的连接?我只想下载一个可见的内容
我很熟悉,但是这个库不支持SSL连接,所以我不能使用它。集合视图有一个委托方法,当单元格消失时调用该方法
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
只要确保您有停止连接的方法,然后调用该方法即可
使用NSURLConnection开始下载。
创建具有一个NSUrlConnection实例属性的NSObject子类,为该子类提供链接,它将使用NSUrlConnection下载映像。
当您想要下载图像并将其推送到数组中时,创建此类的实例(例如ConnectionsArray) 如果您不想下载特定的indexPaths图像,请使用ConnectionsArray取消这些图像。
使用indexPath和ConnectionsArray获取特定的下载实例,并调用该对象的NSURLConnection的cancel方法
NSURLConnection具有取消方法,可取消正在进行的操作。我强烈建议您使用 然后在ViewDiLoad中创建一个
NSOperation
数组:
self.operationQueue = [[NSMultableArray alloc]init];
在您的-(MainVCCell*)集合视图:(UICollectionView*)集合视图cellForItemAtIndexPath:(NSIndexPath*)indexPath
中,制作类似的内容:
AFHTTPRequestOperation *operation [[AFHTTPRequestOperation alloc] initWithRequest:posterURLRequest];
operation.responseSerializer = [AFImageResponseSerializer serializer];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
success:^(NSURLRequest *request, NSHTTPURLResponse *response, UIImage *image)
{
if(cellToUpdate) {
[cellToUpdate.avatarImageView setImage:[UIImage imageWithData:data]];
}
person.avatar = data;
[[CoreDataController sharedInstance] saveContext];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error)
{
// manage errors
}];
[self.operationQueue addObject:operation];
[operation start];
它们位于-(void)collectionView:(UICollectionView*)collectionView DiEndDisplayingCell:(UICollectionViewCell*)单元格中的ItemAtIndexPath:(NSIndexPath*)indexPath
[self.operationQueue[indexPath.row] cancel];
我更习惯于NSURLConnection而不是GDC,所以我不能提供示例代码。谢谢,但我如何使用委托将图像下载到单元格?在何处为其设置委托?在“在索引路径方法处的项的单元格”下,为[download Image]调用单元格中的一个方法,该方法执行所有下载操作。因此下载应在单元格子类中?并附上代表。这是严重的MVC违规行为。你的意思是证书无效,你需要解决这个问题吗?但是你不想编辑SDWebImage代码吗?我不想用它来做三方游戏,因为SDWebImage解决了一个问题,创建了一个更大的问题。我刚刚查看了官方文档:没有看到
AFImageRequestOperation
类。只是编辑了我的答案,他们使用AFImageResponseSerializer将AFImageRequestOperation更改为正常的AFHTTPRequestOperation。是的,我读了一篇文档,但是这种方法不支持SSL基本身份验证,而块也有问题。AFNetworking确实支持SSL基本身份验证,您在谈论块的哪一个问题?当然,您应该对在AFNetworking中使用SSL和基本身份验证做一些研究。