Search code examples
iosobjective-cnsurlconnectionafnetworkingnsdata

AFNetworking can easily return a UIImage, but how do I return NSData?


I want to use this category to create UIImage GIFs from NSData. I don't want to use the dataWithContentsOfURL method as it blocks the main thread, so I want to use AFNetworking as I normally do to get the NSData.

With images, I've always done something like this:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:post.url]];
AFHTTPRequestOperation *imageOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
imageOperation.responseSerializer = [AFImageResponseSerializer serializer];

And then start it after a completion block. But that returns a UIImage. How can I asynchronously get NSData with AFNetworking?


Solution

  • Simply don't use the image serializer. I think this should work:

    imageOperation.responseSerializer = [AFHTTPResponseSerializer serializer];
    

    More about different AFNetworking serialization options here.