xnz
xnz

Reputation: 55

Why I don't get a CGContextRef

I try to get a CGContextRef from UIImage. Always my CGContextRef is 0x0 in Debugger.

Here is the code:

NSString *movName = imgName;
        NSString *path = [[NSBundle mainBundle] pathForResource:movName ofType:@"png"];
        UIImage *image = [[UIImage alloc] initWithContentsOfFile:path];

        CGImageRef imageRef = [image CGImage];
        CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
        CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);

        CGContextRef bitmap = CGBitmapContextCreate(NULL, 
                                                    100, 
                                                    100, 
                                                    CGImageGetBitsPerComponent(imageRef), 
                                                    CGImageGetBytesPerRow(imageRef), 
                                                    colorSpaceInfo, 
                                                    bitmapInfo);

thank you for answering

Upvotes: 0

Views: 1275

Answers (1)

fbrereto
fbrereto

Reputation: 35925

The NULL result is detailed in the documentation:

Return Value:
A new bitmap context, or NULL if a context could not be created

I would start by investigating the parameters you are passing to the function. It is possible the source image isn't being loaded properly. Have you tried debugging this code? What does the debugger tell you?

Upvotes: 2

Related Questions