【发布时间】:2011-12-16 12:11:33
【问题描述】:
我想创建一个画廊。它根据用户选择的类别加载不同的图像。我曾经在 UIImageViews 中填充图像。
我在选择不同类别时不会清除之前选择的图像。这是我用于填充图像的代码。
-(void)refresh{
categoryLabel.text = treatment.treatmentName;
NSMutableArray *galleryImages =[NSMutableArray alloc] ;
galleryImages = [[PatientImage alloc]find:treatment.treatmentId];
int imgCount = [galleryImages count];
for(int i=0;i<imgCount;i++){
PatientImage *apatientImage = [galleryImages objectAtIndex:i];
UIImage *img1 = [UIImage imageNamed:apatientImage.imageBefore];
UIImageView *myImageView = [[UIImageView alloc] initWithImage:img1];
myImageView.contentMode = UIViewContentModeTopRight;
myImageView.frame = CGRectMake(120+i*240,120.0,100.0, 100.0);
[self.view addSubview:myImageView];
UIImage *img2 = [UIImage imageNamed:apatientImage.imageAfter];
UIImageView *myImageView2 = [[UIImageView alloc] initWithImage:img2];
myImageView2.contentMode = UIViewContentModeTopRight;
myImageView2.frame = CGRectMake(120+i*240+300,120.0,100.0, 100.0);
[self.view addSubview:myImageView2];
}
}
【问题讨论】:
-
不回答你的问题,让我做一点代码审查:-):
[[PatientImage alloc]find:treatment.treatmentId];对我来说看起来很奇怪。您的 find 方法是否初始化了一个对象?更好的编码风格是拥有一个具有图像属性的Patient对象:UIImage *patientImage = [[Patient alloc]initWithTreatmentId:treatment.treatmentId].patientImage。alloc和init*也应该永远在一起。或者,您可以像这样获得自动释放的对象:[NSMutableArray array]
标签: objective-c image ipad uiimageview