IOS - DrawRect图像

时间:2012-10-28 03:19:44

标签: objective-c ios uilabel drawrect

我有一个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"];
}

1 个答案:

答案 0 :(得分: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];