문제

나는 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로 텍스처로 사용하려면 어떻게합니까?

도움이 되었습니까?

해결책

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

다른 팁

나는 동일한 문제가 있습니다.이 버그가 이미지를 다시 그릴 것처럼 보입니다.

다음과 같이 cgContext를 만드는 경우 나올 것입니다 :

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

그러나 다음과 같이 cgContext를 만드는 경우 텍스처가 성공적으로로드됩니다.

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

CGImageref에 대한 잘못된 알파 설정으로 인해 동일한 오류가 발생했습니다. GLKTextureLoader의 설명서는 표 1의 지원되는 CGImage 형식을 나열합니다.

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

모든 것이 나에게 괜찮습니다.이것은 Feng Stone의 문제를 해결해야합니다. CGImageCreateWithImageInRect 함수를 건너 뛰고 올바른 매개 변수로 새 CGContext를 생성하는 것이 좋습니다. CGContextDrawImage

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top