后续Q以避免UI滞后(IOS) [英] A follow-up Q for avoid UI getting lagged (IOS)

查看:100
本文介绍了后续Q以避免UI滞后(IOS)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我使用此代码,进行了一些修改:

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];
            });
        }
    });

}

如您所见,实际上,在我得到那张照片之后,我想立即将其设置为我的"photoButton",

As you can see, in fact after I get that photo I want set it immediately to my "photoButton",

但是现在,UI变得平滑了,但是我的photoButton的外观始终是黑色的...

but now, UI got smoothed, but my photoButton's appearance is always black...

下一步我该怎么做?

感谢您的耐心等候.

__ _ __ _ __ _ __ _ ___ 已更新 _ __ _ __ _ _

_______________Updated________

我有2个viewController,A和B.

I have 2 viewControllers, A and B.

A是根viewController,B是A的子viewController.

A is the root viewController, and B is A's child viewController.

在B中,有一个用于调用相机拍照的按钮.

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.

当我从A推一个新的B(没有照片)时,

When I push a new B (with no photo) from A,

一切顺利.

但是当有一个旧的B里面有照片的时候.

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];

}

但是我确实需要在显示视图之前获取照片,因为我需要将该照片设置为photoButton的背景.

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.

非常感谢!

推荐答案

我找到了答案.

只需添加一行代码即可刷新按钮.

Just add a line of code to refresh the button.

对我来说是

[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];

这篇关于后续Q以避免UI滞后(IOS)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆