我正在尝试从 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 的一部分用作纹理?