json からの画像ロードでUIImage
描画したいものがあります。UILabel
描画する前に画像を保存したい。
画像をロードしましたが、描画しません。画像の問題を保存したと思います。
これが私のコードです:
- (void)createDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
itemsFilename = [[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"listEPG"]]];
thumbsFilename = [[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"listImage"]]];
_itemsNames = [[NSMutableArray alloc] initWithContentsOfFile:itemsFilename];
images = [[NSMutableDictionary alloc] initWithContentsOfFile:thumbsFilename];
imagesOperations = [[NSOperationQueue alloc] init];
[imagesOperations setMaxConcurrentOperationCount:1];
if (images == nil) {
images = [[NSMutableDictionary alloc] init];
}
}
- (void)drawRect:(CGRect)rect {
for(int i = 0;i<10;i++) {
NSString* imageUrl= [NSString stringWithFormat:@"%@",[[[data objectAtIndex:i] objectForKey:@"Channel"] objectForKey:@"Image"]];
UIImage *imgLogo = [self setImage:imageUrl];
NSLog(@"%@",imgLogo);
[imgLogo drawInRect:CGRectMake(0,y_lblLogo,70.f,30.f)];
}
}
- (UIImage *) setImage:(NSString *)theUrl {
//NSLog(@"%@",theUrl);
id userImage = [images objectForKey:theUrl];
if(userImage == nil) {
[images setObject:[NSNull null] forKey:theUrl];
GetImage *op = [[GetImage alloc] initWithTheURL:theUrl target:self action:@selector(getImage:)];
NSLog(@"%@",op); //[imagesOperations addOperation:op];
} else if ([userImage isKindOfClass:[NSData class]]) {
return [UIImage imageWithData:userImage];
}
return [UIImage imageNamed:@""];
}
- (void) getImage:(NSDictionary *)info {
NSString* url = [info objectForKey:@"url"];
id image = [info objectForKey:@"image"];
[images setObject:UIImageJPEGRepresentation(image, 0.5) forKey:url];
[images writeToFile:thumbsFilename atomically:YES];
NSMutableSet *filepathsSet = [[NSUserDefaults standardUserDefaults] objectForKey:@"filePathLogoChannel"];
[filepathsSet setByAddingObject:thumbsFilename];
[[NSUserDefaults standardUserDefaults] setObject:filepathsSet forKey:@"filePathLogoChannel"];
}