Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kythanh/9637212ba2e54d6c664b to your computer and use it in GitHub Desktop.
Save kythanh/9637212ba2e54d6c664b to your computer and use it in GitHub Desktop.
Download and save image with AFNetworking on iOS
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: url]];
AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest: request];
requestOperation.responseSerializer = [AFImageResponseSerializer serializer];
[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//NSLog(@"Response: %@", responseObject);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents = [paths objectAtIndex:0];
NSString *finalPath = [documents stringByAppendingPathComponent: fn];
[[operation responseData] writeToFile:finalPath atomically:YES];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Image error: %@ - %@", error, url);
}];
[requestOperation start];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment