UIImageView setImage不会改变图像的外观

时间:2017-03-03 12:52:21

标签: ios xcode uiimageview

我正在编写音源生成器UI,每次生成器开始生成并完成生成时,我需要更改UIImageView图像。 为此,我有

@property (weak, nonatomic) IBOutlet UIImageView *headphonesImage;

在我的UIViewController中。我像这样添加默认图像

- (void) viewDidLoad
{
    ...
    headphonesImagesArray = [NSArray arrayWithObjects:
                             [UIImage imageNamed: @"Both Headphones"],
                             [UIImage imageNamed: @"Both Headphones Playing"],
                             nil];

    [self.headphonesImage setAlpha: 0.5];
    [self.headphonesImage setImage: headphonesImagesArray[0]];
    ...
}

我的音频发生器将消息发送到此方法

-   (void) toneGeneratorControllerStateHasChangedWithNotification: (NSNotification *) notification
{
    if([notification.name isEqualToString: ToneGenerationHasFinished])
        [self.headphonesImage setImage: headphonesImagesArray[0]];
    else
        [self.headphonesImage setImage: headphonesImagesArray[1]];
}

问题在于虽然耳机图像变为[1],但没有任何反应。我可以在变量检查器中看到每次调用方法时都会更改headphonesImage图像,但这些更改不会出现在模拟器和iPhone的屏幕上。我甚至无法隐藏这个该死的UIImageView。 setHidden:true什么都不做。

请帮忙!

4 个答案:

答案 0 :(得分:1)

"我的音频发生器将消息发送到此方法"

如果正在写音频发生器,它听起来像是在后台线程上运行。您应该在主线程上发送通知,特别是如果它们触发UI更改(例如设置图像),例如:

dispatch_async(dispatch_get_main_queue(),^{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"toneGeneratorControllerStateHasChanged" 
                                                        object:nil 
                                                      userInfo:imageDict];
});

或者,您可以像Igor建议的那样将图像更改发送到主线程。缺点是你必须在任何时候使用这种通知,而不是在一个地方进行。

答案 1 :(得分:0)

这样做: -

-   (void) toneGeneratorControllerStateHasChangedWithNotification: (NSNotification *) notification
{
    if([notification.name isEqualToString: ToneGenerationHasFinished]) {
       self.headphonesImage.image = [UIImage imageNamed:@"Both Headphones"];
}
    else {
        self.headphonesImage.image = [UIImage imageNamed:@"Both Headphones Playing"];
}
}

而不是setImage。

答案 2 :(得分:0)

VDL

中的

headphonesImagesArray = [NSArray arrayWithObjects:
                         @"Both Headphones",
                         @"Both Headphones Playing",
                         nil];

这样做:

-   (void) toneGeneratorControllerStateHasChangedWithNotification: (NSNotification *) notification
{
       if([notification.name isEqualToString: ToneGenerationHasFinished]) {
           self.headphonesImage.image = [UIImage imageNamed: headphonesImagesArray[0]];
    }
        else {
            self.headphonesImage.image = [UIImage imageNamed:headphonesImagesArray[1]];
    }
}

答案 3 :(得分:0)

它看起来很阴暗,但似乎我找到了解决方案:

-   (void) toneGeneratorControllerStateHasChangedWithNotification: (NSNotification *) notification
{
    BOOL finished = FALSE;
    if([notification.name isEqualToString: ToneGenerationHasFinished])
        finished = TRUE;
    dispatch_async(dispatch_get_main_queue(),
    ^{
        if(finished)
            [self.headphonesImage setImage: headphonesImagesArray[0]];
        else
            [self.headphonesImage setImage: headphonesImagesArray[1]];
    });
}

据我所知,所有UI绘图都发生在主线程中。看起来通知方法在主线程中不起作用,除非你明确告诉它这样做......

相关问题