0

I have an UIImage which i want to draw in UILabel, image load from json.

I wanna save image before draw it.

I've loaded image but i don't draw it, I think I saved the image problem.

Here is my code:

- (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
  • 23,310
  • 7
  • 60
  • 85
heaven
  • 221
  • 4
  • 19
  • 1
    What exactly is the issue you are facing? Is it that you are not able to save the image? If it is a crash message, please post the stack trace. – iDev Oct 28 '12 at 07:01
  • Can you check my code, i don't draw it, maybe image saving error – heaven Oct 28 '12 at 07:37
  • So my understanding of your issue is that, you have problem in drawing the image and you feel that the issue is with saving image to disc, right? What is the data type of [info objectForKey:@"image"]? Is it UIImage itself? Can you check by printing using an NSLog statement as NSLog(@"%@", [[image class] description]) after the line id image = [info objectForKey:@"image"]; – iDev Oct 28 '12 at 07:59
  • I pasted NSLog after the line id image..., the function getImage doesn't call, i don't know why? I'll check again my code – heaven Oct 28 '12 at 08:43
  • NSLog(@"%@", [[image class] description]) return UIImage – heaven Oct 28 '12 at 08:56

1 Answers1

0

Change

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

to

UIImage *image = (UIImage *)[info objectForKey:@"image"];
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
[imageData writeToFile:thumbsFilename atomically:YES];  
iDev
  • 23,310
  • 7
  • 60
  • 85
  • Unfortunately,i has not drawn image. – heaven Oct 28 '12 at 09:22
  • What is happening now? Is it saving to disc properly? Can you check it? Without seeing the complete project it is difficult to say. – iDev Oct 29 '12 at 02:48
  • Now, i've drawn image but the first time when i load uiscrollview, image's not showing, i think GetImage *op = [[GetImage alloc] initWithTheURL:theUrl target:self action:@selector(getImage:)]; have problem it's not return uiimage? – heaven Oct 29 '12 at 03:02