Objective c Cocoa Touch:在for loop中添加图像

Objective c Cocoa Touch:在for loop中添加图像,objective-c,cocoa-touch,uiimageview,Objective C,Cocoa Touch,Uiimageview,当我放入一个图像URL数组时,这对我来说是个打击,我想这是因为我创建了UIImageView,然后试图用相同的名称再次创建它 - (void)cycleImages:(NSArray *)images { int fromLeft = 5; for(int n = 0; n <= [images count]; n++){ UIImageView *myImageView = [[UIImageView alloc] initWithImage:[[UIImage all

当我放入一个图像URL数组时,这对我来说是个打击,我想这是因为我创建了UIImageView,然后试图用相同的名称再次创建它

- (void)cycleImages:(NSArray *)images {


int fromLeft = 5;
   for(int n = 0; n <= [images count]; n++){

   UIImageView *myImageView = [[UIImageView alloc] initWithImage:[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[images objectAtIndex:n]]]]];

   myImageView.frame = CGRectMake(fromLeft, 5, 48, 48); // from left, from top, height, width

   fromLeft = fromLeft + 53;

   //add image view to view
   [self.view addSubview:myImageView];


    NSLog(@"%@", [images objectAtIndex:n]);

}
}
有什么想法吗? 谢谢 Dex

首先,应该使用而不是索引循环。快速枚举更简单、更清晰、更快速—只要您不特别需要某项内容的索引,就没有理由不使用快速枚举

这假设[images objectAtIndex:n]是一个字符串。这就是你放在数组中的东西吗

此外,由于您使用alloc创建了该图像视图,因此需要将其释放。如果您使用的是ARC,这是自动完成的,因此您无需执行任何操作;否则,您需要在将其添加为子视图后将其发送为release,或者将其发送为autorelease

这是对我的打击

请说得更具体些。你会遇到什么样的“撞车”,在哪条线上

如果您得到一个崩溃日志,请编辑您的问题,将其包括在内

   UIImageView *myImageView = [[UIImageView alloc] initWithImage:[[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[images objectAtIndex:n]]]]];