5

我正在尝试以这种方式使用 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

我无法弄清楚我做错了什么。发生了什么错误?

4

6 回答 6

7

如果您使用 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");
    }];
于 2017-07-05T15:02:54.977 回答
2

您可以使用 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);
    }];

希望这件事有所帮助

于 2016-09-26T06:12:58.073 回答
2

我很抱歉浪费了你们的时间,伙计们。我注意到链接数组中的几个元素是空的,在添加检查条件后,我遇到了与答案内容类型相关的另一个问题:

“不可接受的内容类型:文本/纯文本”

"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];”是正确的。另请记住,您正在使用异步进程和管理器在函数返回值后获得答案。谢谢你们!

于 2016-03-15T10:55:37.683 回答
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];
于 2016-03-15T09:53:49.790 回答
1

图像下载已被重构,以遵循 AlamofireImage 的架构以及AFImageDownloaderAFNetworking 3.0 中的新类。

于 2016-03-15T10:19:09.383 回答
0

使用URLFragmentAllowedCharacterSet代替URLHostAllowedCharacterSet

于 2016-03-15T09:53:13.083 回答