我的代码:
for (int index = 1; index < [[firstPageJSONData objectForKey:@"pillars"] count] ; index++) {
NSString *rotaryName = [NSString stringWithFormat:@"rotaryImage%d", index];
UIImage *rotaryWheel = [self.wheelView valueForKey:rotaryName];
NSString *urlStringImage2 = [[[firstPageJSONData objectForKey:@"pillars"] objectAtIndex:index] objectForKey:@"image_url"];
NSData *imageData2 = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: urlStringImage]];
rotaryWheel = [UIImage imageWithData: imageData];
}
然而,这段代码有效,但它太漫长而且看起来很糟糕。
NSString *urlStringImage = [[[firstPageJSONData objectForKey:@"pillars"] objectAtIndex:0] objectForKey:@"image_url"];
NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: urlStringImage]];
self.wheelView.rotaryImage1 = [UIImage imageWithData:imageData];
NSString *urlStringImage = [[[firstPageJSONData objectForKey:@"pillars"] objectAtIndex:1] objectForKey:@"image_url"];
NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: urlStringImage]];
self.wheelView.rotaryImage2 = [UIImage imageWithData:imageData];
此外,我需要这样做12次!
答案 0 :(得分:1)
更改如下
for (int index = 1; index < [[firstPageJSONData objectForKey:@"pillars"] count] ; index++) {
NSString *rotaryName = [NSString stringWithFormat:@"rotaryImage%d", index];
NSString *urlStringImage2 = [[[firstPageJSONData objectForKey:@"pillars"] objectAtIndex:index] objectForKey:@"image_url"];
NSData *imageData2 = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: urlStringImage]];
[self.wheelView setValue:[UIImage imageWithData: imageData] forKey:rotaryName];
}
答案 1 :(得分:0)
编辑:
for (int index = 1; index < [[firstPageJSONData objectForKey:@"pillars"] count] ; index++) {
NSString *rotaryName = [NSString stringWithFormat:@"rotaryImage%d", index];
NSString *urlStringImage2 = [[[firstPageJSONData objectForKey:@"pillars"] objectAtIndex:index] objectForKey:@"image_url"];
NSData *imageData2 = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: urlStringImage]];
rotaryWheel = [UIImage imageWithData: imageData];
[[self.wheelView setValue: rotaryWheel forKey: rotaryName];
}