使用键值编码 (KVC) 获取 UIImage 但在为其设置图片时不起作用
Using key-value coding (KVC) to obtain a UIImage but doesn't work when setting picture to it
我的代码:
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 次!
已编辑:
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];
}
更改如下
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];
}
我的代码:
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 次!
已编辑:
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];
}
更改如下
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];
}