0

我有一个UIImage我想画的UILabel,从 json 加载的图像。

我想在绘制之前保存图像。

我已经加载了图像,但我没有绘制它,我想我保存了图像问题。

这是我的代码:

- (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"];
}
4

1 回答 1

0

改变

id image = [info objectForKey:@"image"];
[images setObject:UIImageJPEGRepresentation(image, 0.5) forKey:url];
[images writeToFile:thumbsFilename atomically:YES];

UIImage *image = (UIImage *)[info objectForKey:@"image"];
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
[imageData writeToFile:thumbsFilename atomically:YES];  
于 2012-10-28T09:04:08.467 回答