结帐我的工作代码,
NSURL *url = [NSURL URLWithString:imageURL];
NSURLSessionTask *_imageDownloadTask = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
//Here you can read your files from data
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
//Save your files here for cache purpose
@try {
//You can handle onDownloadFinishWithFile: here too using delegate protocol
}
@catch (NSException *exception) {
NSLog(@"%@", exception.reason);
}
@finally {
// Defines a block of related code that is subsequently executed whether an exception is thrown or not.
}
});
}
}
}];
[_imageDownloadTask resume];
[注:我使用上面的代码图像下载]。
更新您与您的代码问题,所以很容易找到一些bug,或者你面对任何类型的错误 –
添加你的代码有助于理解你在做什么错误? –
您应该使用NSURLSessionDownloadTask而不是NSURLSessionTask来下载文件。并且请显示代码。否则,这只是一个猜测游戏,永远不会结束。 – GeneCode