我正在尝试使用AFNetworking 3.0以这种方式下载图像:
- (UIImage *) loadImage:(NSString *) link
{
__block UIImage *image = [UIImage imageNamed:@"no_user_profile_pic.png"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer = [AFImageResponseSerializer serializer];
[manager GET:[link stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLHostAllowedCharacterSet]]
parameters:nil
progress:nil
success:^(NSURLSessionTask *task, id responseObject) {
image = (UIImage *) responseObject;
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Load Image error - %@", [error description]);
}];
return image;
}
每次我收到此错误:
***断言失败 - [AFHTTPRequestSerializer requestWithMethod:URLString:parameters:error:],/ Users /.../ AFNetworking / AFURLRequestSerialization.m:353
我无法弄清楚我做错了什么。发生了什么错误?
答案 0 :(得分:6)
如果您使用AFNetworking 3.0,则需要添加头文件 AFImageDownloader.h :
#import <AFNetworking/AFImageDownloader.h>
并使用方法 downloadImageForURLRequest :
NSString *stringVideo = [NSString stringWithFormat:@"https:%@", your link];
AFImageDownloader *downloader = [[AFImageDownloader alloc] init];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: stringVideo]];
[downloader downloadImageForURLRequest:request success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull responseObject) {
NSLog(@"Succes download image");
}failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
NSLog(@"Error download image");
}];
答案 1 :(得分:2)
我很抱歉花时间,伙计们。我注意到链接数组中的几个元素是空的,在添加了检查条件之后我遇到了与答案内容类型相关的另一个问题:
“不可接受的内容类型:text / plain”
和
“Error Domain = NSCocoaErrorDomain Code = 3840”使用AFNetworking无法完成操作“
在我尝试了几种变种之后:
manager.responseSerializer = [AFImageResponseSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
“manager.responseSerializer = [AFHTTPResponseSerializer serializer];
”是正确的。还要记住,在函数返回值后,您正在使用异步进程和管理器获取答案。谢谢大家!
答案 2 :(得分:1)
你可以使用:
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURL *URL = [NSURL URLWithString:@"http://example.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:nil destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];
return [documentsDirectoryURL URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
NSLog(@"File downloaded to: %@", filePath);
}];
[downloadTask resume];
答案 3 :(得分:1)
图像下载已经过重构,可以使用AFNetworking 3.0中新的AFImageDownloader
类来跟踪AlamofireImage的架构。
答案 4 :(得分:1)
您可以使用AFNetworking的直接方法下载图像
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:[NSURL URLWithString:"YOUR URL OF IMAGE HERE"];
["YOUR IMAGEVIEW" setImageWithURLRequest:request placeholderImage:"YOUR PLACEHOLDER IMAGE" success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) {
"YOUR IMAGEVIEW".image = image;
} failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
希望这件事有所帮助
答案 5 :(得分:0)
使用URLFragmentAllowedCharacterSet
代替URLHostAllowedCharacterSet