Using PDFs for icon images in Xcode 7.2

2019-06-22 15:23发布

问题:

I'm attempting to use PDF files as icons in an app I'm working on. The issue I'm encountering is I'm getting inconsistent tint colors.

If I set a button image from interface builder, the icon image shows up black at runtime. Every time. Regardless of what I attempt to set from interface builder.

I tried setting my button icon image via code and instead of showing up black, it's white:

let myGraphicFile = UIImage(named: "myPDFImage")
let myButtonImage = myGraphicFile?.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate)
myButton.setImage(myButtonImage, forState: .Normal)

From code, regardless of what I attempt to set the tint to, it's always white from code.

I discovered this post relating to Xcode 6.x, but I think it might be dated, as I'm able to partially do it, but I can't set the tint.

Use PDF in XCode for an AppIcon (.appiconset collection)

I create the icons in Inkscape, save as PDF 1.5. I add the file to Images.xcassets. In Images.xcassets' attributes inspector, I'm setting:

Devices to Universal

Scale factor to Single Vector.

Summary: I can get it to show up and scale properly, but it's either black from interface builder or white from code. I suspect I'm missing something re: how to save the file from Inkscape.

Thank you for reading. If you have any suggestions, I welcome them.

回答1:

I have figured out how to create vector icons with Inkscape. When you use PDFs to display icons in iOS, you need to alter the Attributes Inspector for your icon in xcAssets as follows:

1) Drag the PDF into xcAssets

2) Set devices (I did Universal and it worked fine)

3) If your PDF icon is under 1x, 2x, or 3x size class, drag it to Universal and delete the rest of them.

4) Set Scale Factors to Single Vector.

5) Render as Template Image.

Once it's configured there, then you just treat it was you would any other image in interface builder. It's essentially the same thing I was doing in code, but I don't think it gets done in code...it's gotta be done on xcAssets where the image lives. It's my understanding iOS renders vector images for the size class at run time. I think by attempting to tweak it in code wasn't working because the image had already been rendered.

If anyone has any questions on this, I found this link helpful in resolving my issue.

Additionally, this post covers the topic, too. https://stackoverflow.com/a/25804358/4475605