How to use MTKTextureLoader to load png data

I am trying to load some PNG data with MTKTextureLoader newTextureWithData,but the result shows wrong at the alpha area.

Here is the code. I have an image URL, after it downloads successfully, I try to use the data or UIImagePNGRepresentation (image), they all show wrong.

    UIImage *tempImg = [UIImage imageWithData:data];
    CGImageRef cgRef = tempImg.CGImage;
    
    MTKTextureLoader *loader = [[MTKTextureLoader alloc] initWithDevice:device];
    id<MTLTexture> temp1 = [loader newTextureWithData:data options:@{MTKTextureLoaderOptionSRGB: @(NO), MTKTextureLoaderOptionTextureUsage: @(MTLTextureUsageShaderRead), MTKTextureLoaderOptionTextureCPUCacheMode: @(MTLCPUCacheModeWriteCombined)} error:nil];
    NSData *tempData = UIImagePNGRepresentation(tempImg);
    id<MTLTexture> temp2 = [loader newTextureWithData:tempData options:@{MTKTextureLoaderOptionSRGB: @(NO), MTKTextureLoaderOptionTextureUsage: @(MTLTextureUsageShaderRead), MTKTextureLoaderOptionTextureCPUCacheMode: @(MTLCPUCacheModeWriteCombined)} error:nil];
    id<MTLTexture> temp3 = [loader newTextureWithCGImage:cgRef options:@{MTKTextureLoaderOptionSRGB: @(NO), MTKTextureLoaderOptionTextureUsage: @(MTLTextureUsageShaderRead), MTKTextureLoaderOptionTextureCPUCacheMode: @(MTLCPUCacheModeWriteCombined)} error:nil];
}] resume];

below is the origin image and the result from MTKTextureloader

HELP!

Hello,

A couple of questions to understand what's going on:

  1. What is your intention in using "MTKTextureLoaderOptionSRGB: @(NO)"?

  2. Has the source PNG image been gamma corrected?

  3. Does this image appear correctly?

UIImage *tempImg = [UIImage imageWithData:data];

  1. "MTKTextureLoaderOptionSRGB: @(NO)" in most the document say how it should be.
  2. not sure,just a regular image download from internet.
  3. when i use [UIImage imageWithData:data], it appears correctly, but when i use MTKTextureLoader, it appears wrong.

can you use the origin image url that i provide, and use MTKTextureLoader to load it right. here is the url http://lxcode.bs2cdn.yy.com/da3d9ae1-93a0-4149-9977-babee7ad2fb8.png

How to use MTKTextureLoader to load png data
 
 
Q