How to download and set a UIImage not on the main thread
I'm adding an image from a web service. How would I code this so that it's
not on the main thread? I want the view to load first and then the image
to load so that the user doesn't experience any slowness when the detail
view controller loads.
What I'm unsure of is how to add this into the dispatching code.
here's my code so far:
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL
URLWithString:mainImageUrl]];
UIImage *image = [[UIImage alloc] initWithData:imageData];
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(10, 150, 300, 180)];
UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 10,
300, 180)];
[iv setImage:image];
[_scrollView addSubview:v];
[v addSubview:iv];
and this is what I'm thinking I can use for the threading:
dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
0), ^{
// Add code here to do background processing
//
//
dispatch_async( dispatch_get_main_queue(), ^{
// Add code here to update the UI/send notifications based on the
// results of the background processing
});
});
thanks for the help
No comments:
Post a Comment