1

我正在尝试从 UIImage 中裁剪一个区域以与 GLKTextureLoader 一起使用。我可以使用 UIImage 直接设置纹理,如下所示:

- (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);
    }
}

但是,如果我尝试以这种方式裁剪图像:

- (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);
    }
}

我收到此错误:

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

如何使用 GLKTextureLoader 将 UIImage 的一部分用作纹理?

4

3 回答 3

6

最后我让它与 UIImagePNGRepresentation 一起工作,我目前只学习 OpenGL/GLKit - 所以不是专家,但我猜 CGImage 缺少一些颜色空间或纹理所需的其他一些数据。

- (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);
    }
}
于 2012-01-25T12:08:52.153 回答
0

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

于 2013-10-15T11:01:13.137 回答
0

我有同样的问题。似乎这个错误来自我重绘图像。

它会出来,如果我像这样创建 CGContext:

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

但是,如果我这样创建 CGContext,它将成功加载纹理:

UIGraphicsBeginImageContextWithOptions(aImage.size, NO, aImage.scale);
于 2013-10-14T09:38:25.393 回答