Created
May 12, 2014 04:07
-
-
Save kythanh/9637212ba2e54d6c664b to your computer and use it in GitHub Desktop.
Download and save image with AFNetworking on iOS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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