Now I'm using this code, with a little modification:
if (self._photoPath && !self._photo) {
dispatch_queue_t bg_thread = dispatch_queue_create("com.yourcompany.bg_thread", NULL);
dispatch_queue_t main_queue = dispatch_get_main_queue();
dispatch_async(bg_thread,^{
NSData *data = [NSData dataWithContentsOfFile:self._photoPath];
if(data != nil) {
dispatch_async(main_queue,^{
self._photo = [UIImage imageWithData:data];
[self.photoButton setImage:[UIImage imageNamed:@"photoButton.png"] forState:UIControlStateNormal];
});
}
});
}
As you can see, in fact after I get that photo I want set it immediately to my "photoButton", but now, UI got smoothed, but my photoButton's appearance is always black...
What should I do next?
_______________________Updated___________________
I have 2 viewControllers, A and B. A is the root viewController, and B is A's child viewController. In B, there is a button for calling the camera to take a photo.
After user took a photo, the photo's appearance becomes that photo.
When I push a new B (with no photo) from A, things goes smoothly. But when there is an old B with a photo in it, the animation gets a little stuck, caused by the following code I guess:
- (void)viewWillAppear:(BOOL) animated {
if (self._photoPath && !self._photo) {
NSData *data = [NSData dataWithContentsOfFile:self._photoPath];
if(data != nil)
self._photo = [UIImage imageWithData:data];
}
[super viewWillApear];
}
But I do need to get that photo before the view is displayed since I need to set that photo to my photoButton's background.
So, is there a way to avoid sticking the view's animation? Because it really result in bad user experience.
Try fetching the photo in a backgroudn thread (I'm using GCD here):
- (void)viewWillAppear:(BOOL) animated {
if (self._photoPath && !self._photo) {
dispatch_queue_t bg_thread = dispatch_queue_create("com.yourcompany.bg_thread", NULL);
dispatch_queue_t main_queue = dispatch_get_main_queue();
dispatch_async(bg_thread,^{
NSData *data = [NSData dataWithContentsOfFile:self._photoPath];
if(data != nil) {
dispatch_async(main_queue,^{ self._photo = [UIImage imageWithData:data]; });
}
});
}
[super viewWillApear];
}