[英]Using key-value coding (KVC) to obtain a UIImage but doesn't work when setting picture to it
My code: 我的代码:
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];
}
However this code works, but it's way too lengthy and looks horrible. 但是这段代码可以工作,但是它太长了,看起来很可怕。
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];
Also I would need to do this 12 times! 我也需要做12次!
change as below 改变如下
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];
}
edited: 编辑:
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];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.