美文网首页
AFN下载来自网络分享

AFN下载来自网络分享

作者: 跬步千里_LenSky | 来源:发表于2016-06-07 23:52 被阅读45次

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

AFURLSessionManager *manager = [[AFURLSessionManager alloc]initWithSessionConfiguration:configuration];

NSURLRequest *request = [NSURLRequest requestWithURL:_imageUrl];

NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath,NSURLResponse *response) {

NSURL *downloadURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];

return [downloadURL URLByAppendingPathComponent:[response suggestedFilename]];

} completionHandler:^(NSURLResponse *response,NSURL *filePath, NSError *error) {

//此处已经在主线程了

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:filePath]];

self.imageView.image = image;

}];

[downloadTask resume];

相关文章

网友评论

      本文标题:AFN下载来自网络分享

      本文链接:https://www.haomeiwen.com/subject/esqjdttx.html