Ios 使用NSURLConnection异步下载UITableView的图像
我有一个带有customCells的TableView,当用户按下某个单元格上的开始按钮时,加载开始。有很多这样的单元,所以我需要并行(异步)实现下载。 对于图像下载和更新表视图中的单元格,我使用下一个代码:Ios 使用NSURLConnection异步下载UITableView的图像,ios,objective-c,uitableview,asynchronous,Ios,Objective C,Uitableview,Asynchronous,我有一个带有customCells的TableView,当用户按下某个单元格上的开始按钮时,加载开始。有很多这样的单元,所以我需要并行(异步)实现下载。 对于图像下载和更新表视图中的单元格,我使用下一个代码: #define myAsyncQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) 我将此方法包括到异步队列中,我认为该队列应该支持并行下载图像。 -(无效)单击开始索引:(NSInteger)包含数据的单
#define myAsyncQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
我将此方法包括到异步队列中,我认为该队列应该支持并行下载图像。
-(无效)单击开始索引:(NSInteger)包含数据的单元格索引:
(CustomTableViewCell*)data
{
dispatch_async(myAsyncQueue, ^{
self.customCell = data;
self.selectedCell = cellIndex;
ObjectForTableCell* tmp =[self.dataDictionary objectForKey:self.names[cellIndex]];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:tmp.imeageURL]
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60.0];
self.connectionManager = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
});
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
self.urlResponse = response;
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
NSDictionary *dict = httpResponse.allHeaderFields;
NSString *lengthString = [dict valueForKey:@"Content-Length"];
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
NSNumber *length = [formatter numberFromString:lengthString];
self.totalBytes = length.unsignedIntegerValue;
self.imageData = [[NSMutableData alloc] initWithCapacity:self.totalBytes];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.imageData appendData:data];
self.customCell.progressView.progress = ((100.0/self.urlResponse.expectedContentLength)*self.imageData.length)/100;
float per = ((100.0/self.urlResponse.expectedContentLength)*self.imageData.length);
self.customCell.realProgressStatus.text = [NSString stringWithFormat:@"%0.f%%", per];
}
I tried to set this block to queue - main queue - cause its the place where image is already downloaded,
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
dispatch_async(dispatch_get_main_queue(), ^{
self.customCell.realProgressStatus.text = @"Downloaded";
UIImage *img = [UIImage imageWithData:self.imageData];
self.customCell.image.image = img;
self.customCell.tag = self.selectedCell;
});
[self.savedImages setObject:img forKey:self.customCell.nameOfImage.text];
NSNumber *myNum = [NSNumber numberWithInteger:self.selectedCell];
[self.tagsOfCells addObject:myNum];
}
如果没有所有队列(当我评论它时),所有队列都可以正常工作-但一次只有一个队列下载。
但是,当我尝试使用队列实现代码时,它没有下载任何东西。我知道我做错了smh,但我无法定义它
非常感谢您提前提供的帮助。使用中的此示例代码来解决延迟加载问题。使用中的此示例代码来解决延迟加载问题。如果您希望从基础开始,我想您应该从开始,因为大多数实现都已被弃用,并且在iOS之后将不可用9供完整参考和 回到你们的问题上来,你们应该做一些类似的事情,这是从教程中得到的
// 1
NSURLSessionDownloadTask *getImageTask =
[session downloadTaskWithURL:[NSURL URLWithString:imageUrl]
completionHandler:^(NSURL *location,
NSURLResponse *response,
NSError *error) {
// 2
UIImage *downloadedImage =
[UIImage imageWithData:
[NSData dataWithContentsOfURL:location]];
//3
dispatch_async(dispatch_get_main_queue(), ^{
// do stuff with image
_imageWithBlock.image = downloadedImage;
});
}];
// 4
[getImageTask resume];
但我个人的建议是go for,它最适合iOS网络,并在iOS应用程序世界中广泛使用/测试
用于使用AFN网络下载图像
[_imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://i.imgur.com/fVhhR.png"]]
placeholderImage:nil
success:^(NSURLRequest *request , NSHTTPURLResponse *response , UIImage *image ){
NSLog(@"Loaded successfully: %d", [response statusCode]);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
NSLog(@"failed loading: %@", error);
}
];
编辑:使用并发进行异步下载
// get main dispact queue
dispatch_queue_t queue = dispatch_get_main_queue();
// adding downloading task in queue using block
dispatch_async(queue, ^{
NSData* imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
// after download compeletes geting main queue again as there can a possible crash if we assign directly
dispatch_async(dispatch_get_main_queue(), ^{
_imageWithBlock.image = image;
});
});
如果您希望从基础开始,我想您应该从开始,因为大多数实现都已弃用,并且在iOS 9之后将不可用。供完整参考和 回到你们的问题上来,你们应该做一些类似的事情,这是从教程中得到的
// 1
NSURLSessionDownloadTask *getImageTask =
[session downloadTaskWithURL:[NSURL URLWithString:imageUrl]
completionHandler:^(NSURL *location,
NSURLResponse *response,
NSError *error) {
// 2
UIImage *downloadedImage =
[UIImage imageWithData:
[NSData dataWithContentsOfURL:location]];
//3
dispatch_async(dispatch_get_main_queue(), ^{
// do stuff with image
_imageWithBlock.image = downloadedImage;
});
}];
// 4
[getImageTask resume];
但我个人的建议是go for,它最适合iOS网络,并在iOS应用程序世界中广泛使用/测试
用于使用AFN网络下载图像
[_imageView setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://i.imgur.com/fVhhR.png"]]
placeholderImage:nil
success:^(NSURLRequest *request , NSHTTPURLResponse *response , UIImage *image ){
NSLog(@"Loaded successfully: %d", [response statusCode]);
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
NSLog(@"failed loading: %@", error);
}
];
编辑:使用并发进行异步下载
// get main dispact queue
dispatch_queue_t queue = dispatch_get_main_queue();
// adding downloading task in queue using block
dispatch_async(queue, ^{
NSData* imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
// after download compeletes geting main queue again as there can a possible crash if we assign directly
dispatch_async(dispatch_get_main_queue(), ^{
_imageWithBlock.image = image;
});
});
@法希姆,谢谢你的建议,我正在学习,并希望自己实现它,没有任何第三方库,那么至少试着自己实现它myself@Fahim,谢谢你的建议,我正在学习,并希望自己实现它,没有任何第三方库,我们至少自己尝试一下。非常感谢,是的,我知道NSURLConnection已弃用,我将转到NSURLSession,但目前我需要在当前项目中解决此问题,这一定会有帮助(非常感谢您的帮助)。非常感谢,是的,我知道NSURLConnection已弃用,我将转到NSURLSession,但是目前我需要在当前项目中解决这个问题,这一定会有帮助的,非常感谢你的帮助)。