title: UIImage to MTLTexture when downloaded as PNG
body: <p>My use case is that a user takes a photo of themself on their phone, and uploads it to an image hosting service as a JPEG. Other uses can then download that image, and that image is then mapped to a metal texture for use in a game.</p>
  
  <p>My issue is that if i download that image and simply display it in a UIImageView, it looks correct, but when I take the downloaded image and transform it into a metal texture it gets mirrored and rotated 90 degrees clockwise. I understand the image getting mirrored is due to metal having a different coordinate system but I don't understand the rotation issues. When I print the details for the image that has been passed into my function it has all the same orientation details as the UIImageView that is displaying correctly, so I have no idea where the issue is. Attached is my function that gives me my MTLTexture.</p>
  
  <pre><code>- (id&lt;MTLTexture&gt;) createTextureFromImage:(UIImage*) image device:(id&lt;MTLDevice&gt;) 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&lt;MTLTexture&gt; texture = [device newTextureWithDescriptor:texDesc];
  
    [texture replaceRegion:MTLRegionMake2D(0, 0, width, height)
               mipmapLevel:0
                 withBytes:CGBitmapContextGetData(context)
               bytesPerRow:4 * width];
  
    return texture;
  }
  </code></pre>