我有一个UIImage
,我想绘制在UILabel
,从json图像加载。IOS - DrawRect图像
我想在绘制之前保存图像。
我已经加载图像,但我不画它,我想我保存了图像问题。
这里是我的代码:
- (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"];
}
到底是什么,你所面临的问题?是否无法保存图像?如果是崩溃消息,请发布堆栈跟踪。 – iDev
你可以检查我的代码,我不画它,也许图像保存错误 – heaven
所以我对你的问题的理解是,你有绘制图像的问题,你觉得问题是保存图像到光盘,对不对? [info objectForKey:@“image”]的数据类型是什么?它是UIImage本身吗?你可以使用NSLog语句作为NSLog(@“%@”,[[image class] description])在行标识符image = [info objectForKey:@“image”]之后进行打印检查。 – iDev