为 iOS 应用加载基于 SVG 的图像资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43721020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Loading SVG based image assets for iOS app
提问by JK140
I purchased an icon from thenounproject as an SVG image. I then use a macOS program called Gapplinto export this SVG into a PNG image. It comes out as a 100x100 pixel image.
我从 thenounproject 购买了一个图标作为 SVG 图像。然后我使用名为Gapplin的 macOS 程序将此 SVG 导出为 PNG 图像。它以 100x100 像素图像的形式出现。
I then open this PNG image with Preview program and go Tools -> Adjust Size and create 10x10, 20x20 and 30x30 images. I then load these images as 1x, 2x, 3x in Xcode.
然后我用预览程序打开这个 PNG 图像并转到工具 -> 调整大小并创建 10x10、20x20 和 30x30 图像。然后我在 Xcode 中将这些图像加载为 1x、2x、3x。
Is this the correct method?
这是正确的方法吗?
回答by Paulo Mattos
No, it's not the optimal solution.
不,这不是最佳解决方案。
Your current solution works, of course, but it's far from ideal. You are losing (precious!) image quality in doing so (see herefor more details). You can improve your worklflow by:
您当前的解决方案当然有效,但远非理想。这样做会使您失去(宝贵的!)图像质量(有关更多详细信息,请参见此处)。您可以通过以下方式改进工作流程:
exportingall 3 resolutions from the original SVG file, ensuring you get the best possible PNG bitmap from a vector based image source (using Gapplin or some other image app);
or convertingyour SVG to PDF and then importing the PDF vector image file in Xcode(last time I checked, Xcode 8 still didn't have direct support for SVG files so we are stuck with good old PDF for now).
从原始 SVG 文件中导出所有 3 种分辨率,确保您从基于矢量的图像源(使用 Gapplin 或其他一些图像应用程序)中获得最佳的 PNG 位图;
或者将您的 SVG转换为 PDF,然后在 Xcode 中导入 PDF 矢量图像文件(我上次检查时,Xcode 8 仍然没有对 SVG 文件的直接支持,因此我们现在只能使用旧的 PDF)。
Both methods, image quality wise, should produce very similar results and are an improvement from your current workflow.
这两种方法,在图像质量方面,应该产生非常相似的结果,并且是对您当前工作流程的改进。
Regarding app file size, you shouldn't again see a difference from neither method. Even using the last method, Xcode still generates the required assets at build timeand, as such, your app will be carrying around the same image/icon set as the first method.
关于应用程序文件大小,您不应再次看到与这两种方法的区别。即使使用最后一种方法,Xcode 仍会在构建时生成所需的资产,因此,您的应用程序将携带与第一种方法相同的图像/图标集。