我正在尝试在Viewcontroller B中点击一个按钮来获取图像。在收到图像作为响应后,我使用图像的url来填充viewcontroller上的图像视图。但是,它需要很长时间才能填充,并且滚动时挂起。有没有任何方法可以平滑地滚动视图而不会冻结,并且很早就得到响应。
答案 0 :(得分:0)
此行不应位于主队列中
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[[responseArr objectAtIndex:indexPath.row]valueForKey:@"CategoryImage"]]];
你可以改成这样的东西
NSString *getImagePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpeg",n1.Id]];
if([[NSFileManager defaultManager] fileExistsAtPath:getImagePath])
{
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
cell.leftImageV.image =img;
[cell.activity stopAnimating];
cell.activity.hidden=YES;
}
else
{
[cell.activity startAnimating];
cell.activity.hidden=NO;
cell.leftImageV.image = nil;
NSLog(@"xdasdxsadxa %@",n1.mainImgStr);
dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(concurrentQueue, ^{
__block NSData * imageData = nil;
dispatch_sync(concurrentQueue, ^{
imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:n1.mainImgStr]];
//Add the file name
[imageData writeToFile:getImagePath atomically:YES]; //Write the file
});
dispatch_sync(dispatch_get_main_queue(), ^{
if(imageData)
{
[self.collectionView reloadData];
}
});
});
}
答案 1 :(得分:-1)
使用dispatch async进行图像检索,即不要在主队列上进行。
Apple Doc: https://developer.apple.com/documentation/dispatch/1453057-dispatch_async