2016-06-01 115 views
3

我的用例是用户在他们的手机上拍摄了他们自己的照片,并将其上传到图像托管服务作为JPEG。其他用途可以下载该图像,然后该图像被映射到用于游戏的金属质感。UIImage作为PNG下载到MTLTexture

我的问题是,如果我下载该图像并将其显示在UIImageView中,它看起来是正确的,但是当我下载图像并将其转换为金属纹理时,它会被镜像并顺时针旋转90度。我知道获取镜像的图像是由于金属具有不同的坐标系,但我不了解旋转问题。当我打印已传入我的函数的图像的细节时,它具有与正确显示的UIImageView相同的方向细节,因此我不知道问题出在哪里。附加是我的功能,给我我的MTLTexture。

- (id<MTLTexture>) createTextureFromImage:(UIImage*) image device:(id<MTLDevice>) device 
{ 
    image =[UIImage imageWithCGImage:[image CGImage] 
        scale:[image scale] 
        orientation: UIImageOrientationLeft]; 

    NSLog(@"orientation and size and stuff %ld %f %f", (long)image.imageOrientation, image.size.width, image.size.height); 

    CGImageRef imageRef = image.CGImage; 

    size_t width = self.view.frame.size.width; 
    size_t height = self.view.frame.size.height; 

    size_t bitsPerComponent = CGImageGetBitsPerComponent(imageRef); 
    size_t bitsPerPixel = CGImageGetBitsPerPixel(imageRef); 

    CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef); 

    CGImageAlphaInfo alphaInfo = CGImageGetAlphaInfo(imageRef); 

    // NSLog(@"%@ %u", colorSpace, alphaInfo); 

    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | alphaInfo; 
    // NSLog(@"bitmap info %u", bitmapInfo); 


    CGContextRef context = CGBitmapContextCreate(NULL, width, height, bitsPerComponent, (bitsPerPixel/8) * width, colorSpace, bitmapInfo); 

    if(!context) 
    { 
    NSLog(@"Failed to load image, probably an unsupported texture type"); 
    return nil; 
    } 




    CGContextDrawImage(context, CGRectMake(0, 0, width, height), image.CGImage); 


    MTLPixelFormat format = MTLPixelFormatRGBA8Unorm; 

    MTLTextureDescriptor *texDesc = [MTLTextureDescriptor texture2DDescriptorWithPixelFormat:format 
                        width:width 
                        height:height 
                       mipmapped:NO]; 
    id<MTLTexture> texture = [device newTextureWithDescriptor:texDesc]; 

    [texture replaceRegion:MTLRegionMake2D(0, 0, width, height) 
      mipmapLevel:0 
       withBytes:CGBitmapContextGetData(context) 
      bytesPerRow:4 * width]; 

    return texture; 
} 

回答

5

Metal坐标是相反的。但是,你现在有一个更简单的加载与纹理方式MTKTextureLoader

import MetalKit 

let textureLoader = MTKTextureLoader(device: device) 
let texture: MTLTexture = textureLoader.newTextureWithContentsOfURL(filePath, options: nil) 

这会为你创建一个新的texture使用位于filePath图像相应的坐标。如果你不想使用NSURL,你也有newTextureWithDatanewTextureWithCGImage选项。

+0

Hollllly废话!非常感谢你做的这些。你能指点我的文件的方向,还是你知道的地方? – crashlog

+1

肯定的事情。这里是[文档](https://developer.apple.com/library/ios/documentation/MetalKit/Reference/MTKFrameworkReference/index.html)和一些[示例代码](https://developer.apple.com/库文件/ ios/samplecode/MetalKitEssentials/Introduction/Intro.html)用于MetalKit纹理加载器。 – Marius