来自我的UIPickerView的图像消失了

时间:2011-12-27 16:01:33

标签: iphone ios5 xcode4.2 uipickerview

我正在构建一个应用程序,用户可以像开槽机一样旋转UIPickerView。我现在面临的问题是,在运行时,我的pickerView中的图像开始随机消失。然后再次旋转,其中一些又回来了。

这是我用来制作图像数组的代码(在我的项目中,每个数组中有17个图像,但为了节省一点空间我缩小了它):

- (void)viewDidLoad{
    [super viewDidLoad];

    [drinkPickerView_ selectRow:1000 inComponent:0 animated:NO];
    [drinkPickerView_ selectRow:1000 inComponent:1 animated:NO];
    [drinkPickerView_ selectRow:1000 inComponent:2 animated:NO];

    if (managedObjectContext_ == nil) 
    { 
        managedObjectContext_ = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext]; 
    }

    drinkPickerView_.delegate = self;
    drinkPickerView_.dataSource = self;

    UIImage *fris1 = [UIImage imageNamed:@"fris01.PNG"];
    UIImage *fris2 = [UIImage imageNamed:@"fris02.PNG"];
    UIImage *fris3 = [UIImage imageNamed:@"fris03.PNG"];
    UIImage *fris4 = [UIImage imageNamed:@"fris04.PNG"];
    UIImage *fris5 = [UIImage imageNamed:@"fris05.PNG"];

    UIImageView *frisView1 = [[UIImageView alloc]initWithImage:fris1];
    UIImageView *frisView2 = [[UIImageView alloc]initWithImage:fris2];
    UIImageView *frisView3 = [[UIImageView alloc]initWithImage:fris3];
    UIImageView *frisView4 = [[UIImageView alloc]initWithImage:fris4];
    UIImageView *frisView5 = [[UIImageView alloc]initWithImage:fris5];

    UIImage *alcoholisch1 = [UIImage imageNamed:@"alcohol01.PNG"];
    UIImage *alcoholisch2 = [UIImage imageNamed:@"alcohol02.PNG"];
    UIImage *alcoholisch3 = [UIImage imageNamed:@"alcohol03.PNG"];
    UIImage *alcoholisch4 = [UIImage imageNamed:@"alcohol04.PNG"];
    UIImage *alcoholisch5 = [UIImage imageNamed:@"alcohol05.PNG"];    

    UIImageView *alcoholischView1 = [[UIImageView alloc]initWithImage:alcoholisch1];
    UIImageView *alcoholischView2 = [[UIImageView alloc]initWithImage:alcoholisch2];
    UIImageView *alcoholischView3 = [[UIImageView alloc]initWithImage:alcoholisch3];
    UIImageView *alcoholischView4 = [[UIImageView alloc]initWithImage:alcoholisch4];
    UIImageView *alcoholischView5 = [[UIImageView alloc]initWithImage:alcoholisch5];

    UIImage *freaky1 = [UIImage imageNamed:@"freaky01.PNG"];
    UIImage *freaky2 = [UIImage imageNamed:@"freaky02.PNG"];
    UIImage *freaky3 = [UIImage imageNamed:@"freaky03.PNG"];
    UIImage *freaky4 = [UIImage imageNamed:@"freaky04.PNG"];
    UIImage *freaky5 = [UIImage imageNamed:@"freaky05.PNG"];

    UIImageView *freakyView1 = [[UIImageView alloc]initWithImage:freaky1];
    UIImageView *freakyView2 = [[UIImageView alloc]initWithImage:freaky2];
    UIImageView *freakyView3 = [[UIImageView alloc]initWithImage:freaky3];
    UIImageView *freakyView4 = [[UIImageView alloc]initWithImage:freaky4];
    UIImageView *freakyView5 = [[UIImageView alloc]initWithImage:freaky5];


    NSArray *frisImageArray = [[NSArray alloc]initWithObjects:frisView1,frisView2,frisView3,frisView4,frisView5, nil];

    NSArray *alcoholischImageArray = [[NSArray alloc]initWithObjects:alcoholischView1,alcoholischView2,alcoholischView3,alcoholischView4,alcoholischView5, nil];


    NSArray *freakyImageArray = [[NSArray alloc]initWithObjects:freakyView1,freakyView2,freakyView3,freakyView4,freakyView5, nil];


    NSString *frisFieldName = [[NSString alloc]initWithFormat:@"frisDrankenPlaatjesArray"];
    [self setValue:frisImageArray forKey:frisFieldName];


    NSString *alcoholischFieldName = [[NSString alloc]initWithFormat:@"alcoholischeDrankenPlaatjesArray"];
    [self setValue:alcoholischImageArray forKey:alcoholischFieldName];

    NSString *freakyFieldName = [[NSString alloc]initWithFormat:@"freakyDrankenPlaatjesArray"];
    [self setValue:freakyImageArray forKey:freakyFieldName];

}

这是我在pickerView中加载图片的代码:

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row 
forComponent:(NSInteger)component reusingView:(UIView *)view{
    if (component==0) {
        NSString *arrayName = [[NSString alloc]initWithFormat:@"alcoholischeDrankenPlaatjesArray"];
        NSArray *array = [self valueForKey:arrayName];
        return [array objectAtIndex:(row%5)];
    }
    else if(component==1){
        NSString *arrayName = [[NSString alloc]initWithFormat:@"frisDrankenPlaatjesArray"];
        NSArray *array = [self valueForKey:arrayName];
        return [array objectAtIndex:(row%5)];

    }
    else if (component==2){
        NSString *arrayName = [[NSString alloc]initWithFormat:@"freakyDrankenPlaatjesArray"];
        NSArray *array = [self valueForKey:arrayName];
        return [array objectAtIndex:(row%5)];
    }
    else{
        return nil;
    }

}

这是使pickerView旋转的代码:

-(IBAction)rotate:(id)sender{

    int numInRow =1;
    int lastVal = -1;
    for (int i=0;i<3; i++) {
        int newValue = arc4random()%1000;
        if (newValue==lastVal)
            numInRow++;
        else
            numInRow =1;
        lastVal=newValue;
        [drinkPickerView_ selectRow:newValue inComponent:i animated:YES];
        [drinkPickerView_ reloadComponent:i];

    }    
}

我希望这是足够的信息,我希望有人可以帮助我。

3 个答案:

答案 0 :(得分:2)

我有类似的问题。我尝试保留对象,数组,图像,但没有修复它。事实证明,如果我在-pickerView viewForRow:方法中再次填充数组(ala如何在viewDidLoad方法中初始化它们),那么它工作得很好。使用每个方法调用重新填充相同的数组似乎效率低下,但它似乎可以解决问题。

答案 1 :(得分:0)

认为问题在于您初始化UIImages的位置:

UIImage *fris1 = [UIImage imageNamed:@"fris01.PNG"];&lt; - 这是自动释放!

您需要将UIImages设置为属性,以便保留它们。 像这样:

@property (nonatomic,retain) UIImage *fris1;

并在你的.m中合成它们。并将图像应用为:

self.fris1 = [UIImage imageNamed:@"fris01.PNG"];

这样,UIImage在整个应用程序生命周期中都会保留。

答案 2 :(得分:0)

这是来自UIPickerView.h。

/ * 这些方法返回一个普通的UIString或一个视图(例如UILabel)来显示该组件的行。对于视图版本,我们缓存任何隐藏的,因此未使用的视图,并将它们传回以供重用。如果您返回另一个对象,旧的将被释放。视图将以行rect为中心 * /

  • (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
  • (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;

因此,当您的pickerview尝试显示之前显示的uiimageview时,它将释放前一个。这就是为什么你应该为每个组件创建不同的uiimageview。