我有一個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