Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 507
Eugene Avatar asked Mar 15 '16 09:03

Eugene


People also ask

What is afnetworking/afnetworking?

GitHub - AFNetworking/AFNetworking: A delightful networking framework for iOS, macOS, watchOS, and tvOS. Loading status checks… Failed to load latest commit information. Exempt "needs investigation" from stalebot. Fix redirect test: use httpbingo service instead due to postmanlabs/h…

How do I add afnetworking as a dependency to my package?

Once you have your Swift package set up, adding AFNetworking as a dependency is as easy as adding it to the dependencies value of your Package.swift. Note: AFNetworking's Swift package does not include it's UIKit extensions. Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

How do I add afnetworking to my Xcode project?

AFNetworking supports multiple methods for installing the library in a project. To integrate AFNetworking into your Xcode project using CocoaPods, specify it in your Podfile: Once you have your Swift package set up, adding AFNetworking as a dependency is as easy as adding it to the dependencies value of your Package.swift.

How do I report a security vulnerability with afnetworking?

And most of all, thanks to AFNetworking's growing list of contributors. If you believe you have identified a security vulnerability with AFNetworking, you should report it as soon as possible via email to [email protected]. Please do not post it to a public issue tracker.


4 Answers

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");
    }];
like image 164
AndrewSas Avatar answered Sep 18 '22 11:09

AndrewSas


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!

like image 39
Eugene Avatar answered Sep 17 '22 11:09

Eugene


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

like image 41
Jitendra Modi Avatar answered Sep 18 '22 11:09

Jitendra Modi


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];
like image 27
Piyush Avatar answered Sep 16 '22 11:09

Piyush