Skip to content

Instantly share code, notes, and snippets.

@jacksonfdam
Created May 28, 2012 14:31
Show Gist options
  • Save jacksonfdam/2819465 to your computer and use it in GitHub Desktop.
Save jacksonfdam/2819465 to your computer and use it in GitHub Desktop.
Fetching Remote Images
dispatch_queue_t img_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(img_queue, ^{
NSURL *url = [NSURL URLWithString:@"http://imgs.xkcd.com/comics/formal_logic.png"];
NSError *error = nil;
NSData *image = [NSData dataWithContentsOfURL:url options:0 error:&error];
if(!error && image && [image length] > 0) {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"image.png"];
[image writeToFile:path options:0 error:&error];
}
UIImage *img = [UIImage imageWithData:image];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment