A follow-up Q for avoid UI getting lagged (IOS)

最后都变了- 提交于 2019-12-12 03:19:38

问题


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?

Thanks for your patience.

_______________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 toke a photo, the photo's apperance 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 stucked, since 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 stucking the view's animation? Cause it really result in bad user experience.

Thanks a lot!


回答1:


I found the answer.

Just add a line of code to refresh the button.

For me, it is

[self.photoButton setImage:[UIImage imageNamed:@"photoButton.png"] forState:UIControlStateNormal];
NSUInteger p[2] = {1,4};
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:
                              [NSIndexPath indexPathWithIndexes:p length:2]] 
                      withRowAnimation:UITableViewRowAnimationNone];


来源:https://stackoverflow.com/questions/9445859/a-follow-up-q-for-avoid-ui-getting-lagged-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!