CGBitmapContextCreateImage or CGContextDrawImage doesn't work

Discussion in 'iOS Programming' started by MacccUser, Jun 23, 2014.

  1. MacccUser macrumors newbie

    Joined:
    Mar 19, 2014
    #1
    I can't solve this problem. Load image, convert to CGImageRef. Try to get bitmap context and render on the screen.

    Code:
    NSURL *imageFileURL = [NSURL fileURLWithPath:stringIMG];
    
       CGImageSourceRef imageSource = CGImageSourceCreateWithURL((CFURLRef)imageFileURL, NULL);
       CGImageRef imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
    
        NSInteger bitsPerComponent = CGImageGetBitsPerComponent(imageRef);
        NSInteger bitsPerPixel = CGImageGetBitsPerPixel(imageRef);
        CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
        NSInteger bytesPerRow = CGImageGetBytesPerRow(imageRef);
        NSInteger width = CGImageGetWidth(imageRef);
        NSInteger height = CGImageGetHeight(imageRef);
        CGColorSpaceRef colorspace = CGImageGetColorSpace(imageRef);
    
        size_t rawData = bytesPerRow*height;
        unsigned char *data = malloc(rawData);
        memset(data2, 0, rawData);
    
         CGContextRef context = CGBitmapContextCreate(data, width, height, bitsPerComponent, bytesPerRow, colorspace, bitmapInfo);
    
    
        CGImageRef imageRef2 = CGBitmapContextCreateImage(context);
    
       // CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef2);
        UIImage *result = [UIImage imageWithCGImage:imageRef2];//if i do like this i got white empty screen
    
        UIImageView *image = [[UIImageView alloc] initWithImage:result];
        [self.view addSubview:image];// if i do like this i got black rectangle on the white screen
    
    I have no idea. I check by breakpoint that context is not null. I don't know what should i do. Please maybe anyone can help me?
     
  2. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #2
    What is your goal? Why do you want a bitmap context?

    Most of the code you show can easily be replaced with [UIImage imageWithContentsOfFile:]
     

Share This Page