2016-06-01 28 views
3

私の使用事例は、ユーザーが自分の電話で自分の写真を撮ってJPEGとしてイメージホスティングサービスにアップロードするというケースです。他の用途ではその画像をダウンロードすることができ、その画像はゲームで使用するために金属テクスチャにマッピングされます。UIImageからMTLTextureをPNG形式でダウンロードした場合

私の問題は、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) 

これはfilePathにある画像を使用して、適切な座標であなたのための新しいtextureを作成します。 NSURLを使用しない場合は、newTextureWithDatanewTextureWithCGImageのオプションもあります。

+0

Hollllly crap !.これをありがとうございました。あなたはドキュメントの方向、またはそれを学んだどこにでも私を向けることができますか? – crashlog

+1

確かに。ここには[documentation](https://developer.apple.com/library/ios/documentation/MetalKit/Reference/MTKFrameworkReference/index.html)といくつかの[sample code](https://developer.apple.com/ライブラリ/ ios/samplecode/MetalKitEssentials/Introduction/Intro.html)に 'MetalKit'テクスチャローダを追加しました。 – Marius

関連する問題