从web服务器中添加一个图片。怎么设置它不在主线程上?我想要视图先加载,然后图片加载。这样用户在体验时不会感到很慢。
就是不知道怎么加到代码中。
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];
我想到可以加到线程中的:
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
});
});