AFNetworking 3.0 can't download image
Asked Answered
J

6

5

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?

Jinajingle answered 15/3, 2016 at 9:29 Comment(4)
From the error log, it seems that the 'link' could be empty.Average
stringByAddingPercentEncodingWithAllowedCharacters: will encrypt the URL including the http://, this wil become http%3A%2F%2F and there for is no longer a valid URL.Allwein
Try adding Exception breakpoint to get the exact line where the error is.Average
@Bharat Modi thank you.Jinajingle
M
7

If you use AFNetworking 3.0 you need add the header file AFImageDownloader.h:

#import <AFNetworking/AFImageDownloader.h>

and use the method 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");
    }];
Maurya answered 5/7, 2017 at 15:2 Comment(1)
IMO it's better to use [AFImageDownloader defaultInstance] to use a shared instance of AFImageDownloader.Nonalignment
J
2

I am sorry for spending your time, guys. I've noticed that couple elements from links array was empty, after was added check condition I've faced with another problem related with the answer content type:

"unacceptable content-type: text/plain"

and

"Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed using AFNetworking"

after I've tried couple variants:

manager.responseSerializer = [AFImageResponseSerializer serializer];
manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];

the "manager.responseSerializer = [AFHTTPResponseSerializer serializer];" was correct. Also keep in mind that you are using async process and manager getting answer after function return the value. Thank you all!

Jinajingle answered 15/3, 2016 at 10:55 Comment(0)
S
2

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

Subir answered 26/9, 2016 at 6:12 Comment(0)
I
1

you can use :

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];
Infringe answered 15/3, 2016 at 9:53 Comment(0)
W
1

Image downloading has been refactored to follow the architecture from AlamofireImage with the new AFImageDownloader class in AFNetworking 3.0.

Wildlife answered 15/3, 2016 at 10:19 Comment(0)
N
0

Use URLFragmentAllowedCharacterSet instead of URLHostAllowedCharacterSet

Neisa answered 15/3, 2016 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.