【问题标题】:UIButton in firstViewController does not change image properly when closing SecondViewController关闭 SecondViewController 时,firstViewController 中的 UIButton 无法正确更改图像
【发布时间】:2012-11-16 10:39:20
【问题描述】:

我正在从 SecondView 控制器中保存图像并调整它的大小。
然后当我回到 Firstview 控制器时,这个控制器上的按钮图像必须 从刚刚保存的图像文件中加载。

图像部分显示(仅图像的顶部)。但是如果我再次进入 SecondViewController 并且不再保存图像并返回到 First ViewController ,则 Button 图像完全出现并且看起来不错。

- (void)showImageFirst { 

    //Show thumb image in button.

    NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
    NSString *docDirectory = [sysPaths objectAtIndex:0];
    NSString *filePath = [NSString stringWithFormat:@"%@/thumbtest.png", docDirectory];
    UIImage *tempimage = [[UIImage alloc] initWithContentsOfFile:filePath];

    [imageForButton setImage:tempimage forState:UIControlStateNormal];
}

我在 ViewDiDLoad 中调用它,我也尝试将它放在 ViewDidAppear 中:

-(void)viewWillAppear {
    [self.showImageFirst];
    [super viewWillAppear:animated];
}

【问题讨论】:

    标签: ios uiviewcontroller uibutton viewdidload


    【解决方案1】:

    您可以让第一个视图控制器成为第二个视图控制器的委托。

    然后,无需将图像保存到磁盘,然后从磁盘加载回它,只需将 UIImage 从第二个视图控制器传递回第一个视图控制器。

    我还没有检查,但我几乎可以肯定,将图像保存到磁盘是异步运行的,因此当您尝试再次加载图像时,可能没有保存图像。

    【讨论】:

    • 非常感谢!好主意!我在 1 中执行了 2 个操作(在查看 segued 时保存和委托)并且 id 不起作用! ) 现在好了! ))
    • 很好 :) 喜欢它,因为你可以优雅地工作并且可以用更少的代码做更多的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    • 1970-01-01
    相关资源
    最近更新 更多