Eugene
Eugene

Reputation: 155

AFNetworking 3.0 can't download image

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?

Upvotes: 5

Views: 6943

Answers (6)

AndrewSas
AndrewSas

Reputation: 843

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

Upvotes: 7

Jitendra Modi
Jitendra Modi

Reputation: 2394

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

Upvotes: 2

Eugene
Eugene

Reputation: 155

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!

Upvotes: 2

UdayM
UdayM

Reputation: 1783

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

Upvotes: 1

Piyush
Piyush

Reputation: 1544

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

Upvotes: 1

Siju
Siju

Reputation: 510

Use URLFragmentAllowedCharacterSet instead of URLHostAllowedCharacterSet

Upvotes: 0

Related Questions