我的Xcode项目中有两个for循环,用于更改9 UIImageViews中的图像。这些UIImageViews中的图像是从服务器下载的,然后显示出来。
My循环使用3个不同的整数来确定要显示什么图像:接下来,current_photo和以前是整数。
我有两个UIButtons,它控制显示下一组和上一组图像。
当我想显示下一组图像时,我创建并使用了以下for循环:
NSArray *imageViews = @[picview_1, picview_2, picview_3, picview_4, picview_5, picview_6, picview_7, picview_8, picview_9];
for (next = current_photo; next < (current_photo+9); next++) {
NSString *next_set = [NSString stringWithFormat:@"%@%i.%@", SERVER_URL, next+1, FORMAT_TYPE];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: next_set]];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[[imageViews objectAtIndex:(next-9)] setImage:image];
}
current_photo = next;这个循环工作得很完美,所有的9个UIImagesViews都改变了图像。
但是,当我想在9 UIImageViews中显示前一组图像时,以下for循环由于某种原因不能正常工作:
NSArray *imageViews = @[picview_1, picview_2, picview_3, picview_4, picview_5, picview_6, picview_7, picview_8, picview_9];
for (previous = current_photo; previous > (previous-9); previous--) {
NSString *next_set = [NSString stringWithFormat:@"%@%i.%@", SERVER_URL, previous+1, FORMAT_TYPE];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: next_set]];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[[imageViews objectAtIndex:(previous-previous)] setImage:image];
}
current_photo = previous;我的for循环有什么问题?请解释一下。
下面是打开我的应用程序时会发生的情况:

下面是按下next按钮时发生的情况:

最后,当按下后退按钮时,应用程序就会冻结.
为什么?出什么事了?请帮帮忙。
谢谢你抽出时间:)
发布于 2013-07-27 16:27:45
原因之一是:
[[imageViews objectAtIndex:(previous-previous)] setImage:image];总是将图像设置为,其他任何一个都不会为它们设置任何设置。
也是
(previous = current_photo; previous > (previous-9); previous--) 将永远循环!每次你做前一次,你比较的东西知道什么时候停止,前一次-9也下降了一次。
我建议这样做:
for (previous = current_photo; previous > (current_photo-9); previous--) {
NSString *next_set = [NSString stringWithFormat:@"%@%i.%@", SERVER_URL, previous+1, FORMAT_TYPE];
NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString: next_set]];
UIImage *image = [[UIImage alloc] initWithData:imageData];
[[imageViews objectAtIndex:(8 - (current_photo - previous))] setImage:image];
}https://stackoverflow.com/questions/17899881
复制相似问题