裁剪 UIImage 以与 GLKTextureLoader 一起使用

发布于 2024-12-28 15:44:31 字数 1556 浏览 3 评论 0原文

我正在尝试从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

女中豪杰 2025-01-04 15:44:31

最后我让它与 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);
    }
}

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);
    }
}
旧话新听 2025-01-04 15:44:31

我有同样的问题。看来这个错误来自于我重新绘制图像。

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

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

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

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

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);
分開簡單 2025-01-04 15:44:31

由于 CGImageRef 的 alpha 设置不正确,我也遇到了同样的错误。
GLKTextureLoader 的文档在表 1 中列出了支持的 CGImage 格式。

苹果文档

我已将用于创建上下文的代码从更改为

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

现在

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

一切对我来说都很好。这应该可以解决冯石的问题。
对于您来说,我建议跳过 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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文