i want to learn GCD on my own app-project and i’ve got problem with it. i think is easy but i don’t know how solve it. so i’ve got 2 methods:
- (void)viewDidLoad
{
[super viewDidLoad];
queue = dispatch_queue_create("com.fe.effect.load", NULL);
__block UIImage *th;
dispatch_sync(queue, ^{
th = [self doThumbWithImage:thumbnail andTag:1];
UIButton *btn1 = [self buttonWithFrame:CGRectMake(0, 0, 200, 100) andBackgroundImage:th andSelector:@selector(doEffect:) andTag:1];
[self.view addSubview:btn1];
});
dispatch_sync(queue, ^{
th = [self doThumbWithImage:thumbnail andTag:2];
UIButton *btn2 = [self buttonWithFrame:CGRectMake(0, 100, 200, 100) andBackgroundImage:th andSelector:@selector(doEffect:) andTag:2];
[self.view addSubview:btn2];
});
}
and 2 method:
-(UIImage *)doThumbWithImage:(UIImage *)_img andTag:(int)_tag {
ImageProcessing *ip = [[ImageProcessing alloc] initWithImage:_img andTag:_tag];
[ip doImageProcessing];
_img = ip.image;
ip = nil;
return _img;
}
And in doThumbWithImage:andTag: i’ve got ImageProcessing class where i do something with my thumbnail object.
when i use dispatch_sync, time for this operation is the same as without GCD. When i use dispatch_async, i can’t see thumbanils of buttons. I know something is wrong, but i don’t know what.
How can i repair this?
thank you for help.
You probably need to do the addSubview in the main thread. You can nest the dispatch_async calls to handle that.