AFNetworking 3.0 can't download image

Viewed 6777

I am trying to download an image using AFNetworking 3.0 doing this way:

- (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;
}

and every time I am getting this error:

*** Assertion failure in -[AFHTTPRequestSerializer requestWithMethod:URLString:parameters:error:], /Users/.../AFNetworking/AFURLRequestSerialization.m:353

I can't figure out what I am doing wrong. What is that error happening?

6 Answers

You can use direct method of AFNetworking to download image

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

Hope this thing helps

Related