2011-10-21 74 views
0

当我放入一个图像url阵列时,这会崩溃,我认为它是因为我创建了UIImageView,然后尝试用相同的名称再次尝试!Cocoa Touch:在for循环中添加图像

- (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

回答

0

首先,您应该使用fast enumeration而不是索引循环。快速枚举更简单,更清晰,更快速 - 只要您不特别需要某个索引,就没有理由不使用快速枚举。

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

这假定[images objectAtIndex:n]是一个字符串。这是你把什么放入数组中?

此外,由于您使用alloc创建了该图像视图,因此需要将其释放。如果你使用ARC,那是自动完成的,所以你不需要做任何事情;否则,您需要在将其添加为子视图后发送它release或将其发送autorelease

这是崩溃的我...

请再具体些。你会得到什么样的“崩溃”,以及什么?

如果您收到崩溃日志,请编辑您的问题以包含它。