Last active
December 23, 2015 03:19
-
-
Save 0oneo/6572272 to your computer and use it in GitHub Desktop.
a code segment from http://jeffreysambells.com/2013/03/01/asynchronous-operations-in-ios-with-grand-central-dispatch
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
NSArray *images = @[@"http://example.com/image1.png", | |
@"http://example.com/image2.png", | |
@"http://example.com/image3.png", | |
@"http://example.com/image4.png"]; | |
dispatch_queue_t imageQueue = dispatch_queue_create("Image Queue",NULL); | |
for (NSString *urlString in images) { | |
dispatch_async(imageQueue, ^{ | |
NSURL *url = [NSURL URLWithString:urlString]; | |
NSData *imageData = [NSData dataWithContentsOfURL:url]; | |
UIImage *image = [UIImage imageWithData:imageData]; | |
NSUInteger imageIndex = [images indexOfObject:urlString]; | |
UIImageView *imageVIew = (UIImageView *)[self.view viewWithTag:imageIndex]; | |
if (!imageView) return; | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
// Update the UI | |
[imageVIew setImage:image]; | |
}); | |
}); | |
} | |
// Continue doing other stuff while images load |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
blog url