Question

I am trying to crop an area out of a UIImage to use with GLKTextureLoader. I can set the texture directly using the the UIImage with the following:

- (void)setTextureImage:(UIImage *)image
{
    NSError *error;

    texture = [GLKTextureLoader textureWithCGImage:image.CGImage options:nil error:&error];

    if(error)
    {
        NSLog(@"Texture Error:%@", error);
    } else {
        self.textureCoordinates[0] = GLKVector2Make(1.0f, 1.0f);
        self.textureCoordinates[1] = GLKVector2Make(1.0f, 0);
        self.textureCoordinates[2] = GLKVector2Make(0, 0);
        self.textureCoordinates[3] = GLKVector2Make(0, 1.0f);
    }
}

However, if I try crop the image this way:

- (void)setTextureImage:(UIImage *)image
{
    NSError *error;

    CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(0.0, 64.0f, 64.0f, 64.0f));
    texture = [GLKTextureLoader textureWithCGImage:imageRef options:nil error:&error];

    if(error)
    {
        NSLog(@"[SF] Texture Error:%@", error);
    } else {
        self.textureCoordinates[0] = GLKVector2Make(1.0f, 1.0f);
        self.textureCoordinates[1] = GLKVector2Make(1.0f, 0);
        self.textureCoordinates[2] = GLKVector2Make(0, 0);
        self.textureCoordinates[3] = GLKVector2Make(0, 1.0f);
    }
}

I get this error:

Texture Error:Error Domain=GLKTextureLoaderErrorDomain Code=12 "The operation couldn’t be completed. (GLKTextureLoaderErrorDomain error 12.)" UserInfo=0x6a6b550 {GLKTextureLoaderErrorKey=Image decoding failed}

How can I take a section of the UIImage to use as a texture with GLKTextureLoader?

Was it helpful?

Solution

In the end I got it to work with UIImagePNGRepresentation, I'm only learning OpenGL/GLKit at the moment - so not an expert but I'm guessing the CGImage was missing some colorspace or some other data that is required for textures.

- (void)setTextureImage:(UIImage *)image withFrame:(CGRect)rect
{
    NSError *error;

    CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage, rect);
    image = [UIImage imageWithData:UIImagePNGRepresentation([UIImage imageWithCGImage:imageRef])];

    texture = [GLKTextureLoader textureWithCGImage:image.CGImage options:nil error:&error];

    if(error)
    {
        NSLog(@"[SF] Texture Error:%@", error);
    } else {
        self.textureCoordinates[0] = GLKVector2Make(1.0f, 1.0f);
        self.textureCoordinates[1] = GLKVector2Make(1.0f, 0);
        self.textureCoordinates[2] = GLKVector2Make(0, 0);
        self.textureCoordinates[3] = GLKVector2Make(0, 1.0f);
    }
}

OTHER TIPS

I have the same issue.It seems like this bug comes from that I redraw the image.

It will come out,if I create the CGContext like this:

CGBitmapContextCreate(NULL, width, height, 8, 4 * width, colorSpace, kCGBitmapByteOrderDefault|kCGImageAlphaPremultipliedLast);

However,it will load texture successfully,if I create the CGContext like this:

UIGraphicsBeginImageContextWithOptions(aImage.size, NO, aImage.scale);

I've had the same error because of incorrect alpha settings for an CGImageRef. The documentation for GLKTextureLoader lists the supported CGImage formats in Table 1.

Apple Documentation

I've changed code used to create context from

CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, width * (CGColorSpaceGetNumberOfComponents(space) + 1), space, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedLast);

to

CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, width * (CGColorSpaceGetNumberOfComponents(space) + 1), space, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedFirst);

Everything is fine for me now. And this should fix Feng Stone's problem. For you I would recommend to skip CGImageCreateWithImageInRect function and create a new CGContext with correct parameters, where you can draw your image with CGContextDrawImage

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top