需要查看哪个UIImageView为空并在那里放置图像

时间:2011-12-17 23:32:26

标签: ios uiimageview

这是我的问题。我有3个UIImageViews默认是空的,放在最初隐藏的UIView上。

当用户选择某些内容时,它们应该被一个接一个地填充。 我知道这听起来很混乱,但我认为下面的代码会清除掉:

- (IBAction)selectButtonPressed:(id)sender {


 if ([labelText.text isEqualToString:@"some text"]) {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"someImage"] ofType:@"png"];     
    UIImage * image = (UIImage * )[UIImage imageWithContentsOfFile:filePath];



    if (firstImage.image == nil) {
        firstImage.image = image;
        imagesView.hidden = YES;
         firstImage.hidden = NO;

    } // places it in the first Image View if it's empty ...
    else if (secondImage.image == nil) {
        secondImage.image = image;
        imagesView.hidden = YES;   
        secondImage.hidden = NO;

    } // ... or in the second one if empty
    else {
        thirdImage.image = image;
         imagesView.hidden = YES;
        thirdImage.hidden = NO; // ... else in the third one

    }
    [filePath release];
    [image release];

}



// then does the same thing depending on the users selection.
      else if ([labelText.text isEqualToString:@"some other text"]) {
        NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString           stringWithFormat:@"someOtherImage"] ofType:@"png"];     
        UIImage * image = (UIImage * )[UIImage imageWithContentsOfFile:filePath];

    if (firstImage.image == nil) {
        firstImage.image = image;
        imagesView.hidden = YES;
        firstImage.hidden = NO;

    }
    else if (secondImage.image == nil) {
        secondImage.image = image;
        imagesView.hidden = YES;
        secondImage.hidden = NO;

    }
    else {
        thirdImage.image = image;
        imagesView.hidden = YES;
        thirdImage.hidden = NO;

    }
    [filePath release];
    [image release];

}
 // and same thing below if none of the above.

else {
    NSString *filePath = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"yet another text"] ofType:@"png"];     
    UIImage * image = (UIImage * )[UIImage imageWithContentsOfFile:filePath];
    if (firstImage.image == nil) {
        firstImage.image = image;
        imagesView.hidden = YES;
        firstImage.hidden = NO;
    }
    else if (secondImage.image == nil) {
        secondImage.image = image;
        imagesView.hidden = YES;
        secondImage.hidden = NO;
    }
    else {
        thirdImage.image = image;
        imagesView.hidden = YES;
        thirdImage.hidden = NO;
    }
    [filePath release];
    [image release];

}  // Code for checking which Image View is empty and place the image there. 

 } 

因此,当我进行第一次选择时,它会启动并正常工作。但对于第二个,它说EXC_BAD_ACCESS并崩溃。有时它适用于前两个选项,而第三个选项则会崩溃。

如果它令人困惑,请告诉我,我会尽可能清楚地解释我的意图。

非常感谢

更新:嗯,我认为当你提出正确的问题时会出现答案。

在我使用firstImage.image = [UIImage imageNamed:@“someImage”]后,一切运作良好;而不是整个NSBundle方法。代码变得更干净,就像一个魅力。

但是我不确定这是否有任何缺点。所以,如果你们可以向我发出任何警告,那我就全力以赴。

再次感谢。

1 个答案:

答案 0 :(得分:0)

您的问题可能与您的内存管理有关。你为那些imageview使用了属性吗?此外,您不需要释放filePath和image,因为您没有使用alloc等方法来初始化它。