您的当前位置:首页正文

AFN下载来自网络分享

来源:花图问答

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];