Jump to content
You must now use your email address to sign in [click for more info] ×

Export Bug: PNG does not load into iOS Xcode metalKit if a layer is not at at least 1% opacity.


CalebK

Recommended Posts

Before I begin it is possible that this is a bug with Metal or MetalKit and not Affinity... However based on a few things my suspicion is that it is Affinity.

 

So here is what happened. I created an ellipse with a radial gradient to transparency. I rasterized it. And tried to export it. I added it to my assets in Xcode and oddly enough it failed to load.

 

Thinking that was odd I substituted in a picture I knew would load and it worked.....

 

So I determined I must be exporting it wrong. 

 

I then open that png (that worked) in affinity resize the canvas and superimpose my transparent circle on it. I export it and put it in assets. It loads perfectly of course there is an unwanted background to this image.

 

I then hide the layer and re-export thinking this will work. It did not. Instead it still failed to load into my app.

Not even setting the layers opacity to 0% worked. The layer had to be at 1% opacity in order for the exported photo to work with my app.

 

To summarize:

For some reason I cant export a photo in a format that MetalKit will read and successfully load unless I add a layer that has a png that has an export that worked/loaded in at at least 1% opacity.

This is 100% reproducible for me.

Attached is a photo that would fail to load in Xcode and also a photo that did load in Xcode. I have also attached the affinity photo file that produces this un-importable image.

The gradient is white and thus appears like a blank image here.

 

And for those of you interested the image is loaded in through this code:

- (id<MTLTexture>) textureFor:(NSString*)name {
     MTKTextureLoader* textureLoader = [[MTKTextureLoader alloc] initWithDevice:device];
     NSDictionary *textureLoaderOptions =
     @{
          MTKTextureLoaderOptionTextureUsage : @(MTLTextureUsageShaderRead),
          MTKTextureLoaderOptionTextureStorageMode : @(MTLStorageModePrivate)
     };
 
     return [textureLoader newTextureWithName:name
     scaleFactor:1.0
     bundle:nil
     options:textureLoaderOptions
     error:nil];
}

I have also attached a zip file of an Xcode project (Version 11.3 (11C29)) ReproduceMetal.zip. If you build to your device it will crash with the image willFail which is currently loaded in.

thisWillFail.afphoto

willFail.png

willPass.png

ReproduceMetal.zip

Link to comment
Share on other sites

  • 2 weeks later...
  • Staff

Apple have known of this issue and reproduced it in 2017 - apparently their loader fails for greyscale/monochrome images (ie RGB images where the image contains only greys) when there is 0% transparency in the image. They have 2 workarounds - the first is to make the image slightly off-monochrome (!!!) and the second, better one, is:

Store the image as an MTLTexture asset in Xcode and then the conversion is done at build time, not runtime and that will work.

Hope that helps! :)

Link to comment
Share on other sites

7 hours ago, MattP said:

Apple have known of this issue and reproduced it in 2017 - apparently their loader fails for greyscale/monochrome images (ie RGB images where the image contains only greys) when there is 0% transparency in the image. They have 2 workarounds - the first is to make the image slightly off-monochrome (!!!) and the second, better one, is:

Store the image as an MTLTexture asset in Xcode and then the conversion is done at build time, not runtime and that will work.

Hope that helps! :)

Helps immensely thank you!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.