裁剪 UIImage 以与 GLKTextureLoader 一起使用
我正在尝试从 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}
如何将 UIImage 的一部分用作 GLKTextureLoader 的纹理?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最后我让它与 UIImagePNGRepresentation 一起使用,我目前只学习 OpenGL/GLKit - 所以不是专家,但我猜测 CGImage 缺少一些色彩空间或纹理所需的一些其他数据。
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.
我有同样的问题。看来这个错误来自于我重新绘制图像。
如果我像这样创建 CGContext ,它就会出来:
但是,如果我像这样创建 CGContext ,它将成功加载纹理:
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:
However,it will load texture successfully,if I create the CGContext like this:
由于 CGImageRef 的 alpha 设置不正确,我也遇到了同样的错误。
GLKTextureLoader 的文档在表 1 中列出了支持的 CGImage 格式。
苹果文档
我已将用于创建上下文的代码从更改为
现在
一切对我来说都很好。这应该可以解决冯石的问题。
对于您来说,我建议跳过 CGImageCreateWithImageInRect 函数并使用正确的参数创建一个新的 CGContext,您可以在其中使用 CGContextDrawImage 绘制图像
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
to
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 withCGContextDrawImage