簡體   English   中英

裁剪要與GLKTextureLoader一起使用的UIImage

[英]Crop a UIImage to use with 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的紋理?

最后,我將其與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的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繪制圖像

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM