When user taps on button im navigating to the another view,and set the background image for that view.But the image is from URL.To convert into NSData format its taking some time.So,How can i achieve this using blocks.
I have used below code but no luck..
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self setBackGroundImageView];
});
- (void)setBackGroundImageView {
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:sharedDelegate.fbAppScopeId]];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
if (imageData) {
UIImageView *backgroundView = [[UIImageView alloc] initWithImage:[self squareImageWithImage:[UIImage imageWithData:imageData] scaledToSize:CGSizeMake(100, 100)]];
CGFloat ratio = backgroundView.frame.size.width/screenWidth;
CGRect imageFrame = backgroundView.frame;
imageFrame.size.width = screenWidth;
imageFrame.size.height = backgroundView.frame.size.height/ratio;
if (imageFrame.size.height<screenHeight) {
ratio = backgroundView.frame.size.height/screenHeight;
imageFrame.size.height = screenHeight;
imageFrame.size.width = backgroundView.frame.size.width/ratio;
}
backgroundView.frame = imageFrame;
backgroundView.center = CGPointMake(screenWidth/2, backgroundView.center.y);
backgroundView.alpha = 0.5;
[self.view addSubview:backgroundView];
}
}
}
You can't do UI Events (adding or removing views) on another thread other than Main Thread. If you do , that might misbehave.
If you still want to do so , add imageView on main thread and set its image in block.
P.S. You can preload (on app startup) image data using block, once it done you can use it as
imageView.image=preLoadImage;.