1

GLKTextureLoaderで使用するために、UIImageから領域を切り抜こうとしています。次の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でテクスチャとして使用するにはどうすればよいですか?

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

同じ問題があります。このバグは、画像を再描画したことが原因のようです。

このようにCGContextを作成すると、次のようになります。

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

ただし、次のようにCGContextを作成すると、テクスチャは正常にロードされます。

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

CGImageRefのアルファ設定が正しくないため、同じエラーが発生しました。GLKTextureLoaderのドキュメントには、サポートされているCGImage形式が表1にリストされています。

Appleのドキュメント

コンテキストの作成に使用するコードを変更しました

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

今はすべてが順調です。そして、これはFengStoneの問題を修正するはずです。関数をスキップCGImageCreateWithImageInRectして、正しいパラメーターを使用して新しいCGContextを作成することをお勧めします。ここで、CGContextDrawImage

于 2013-10-15T11:01:13.137 に答える