简体   繁体   English

如何在iOS中将jpg或png转换为svg?

[英]How to convert jpg or png to svg in iOS?

I like to convert an .jpg or .png file to an .svg format that can be displayed in UIImageView. 我喜欢将.jpg或.png文件转换为可以在UIImageView中显示的.svg格式。 Is there a way to do this in Objective-C? 有没有办法在Objective-C中做到这一点?

You shouldn't use SVG images in Xcode: 不应该在Xcode中使用SVG图像:

it is recommended that you use PNG or JPEG files for most images in your app. 建议您为应用中的大多数图像使用PNG或JPEG文件。 Image objects are optimized for reading and displaying both formats, and those formats offer better performance than most other image formats. 图像对象针对读取和显示两种格式进行了优化,这些格式提供了比大多数其他图像格式更好的性能。 Because the PNG format is lossless, it is especially recommended for the images you use in your app's interface. 由于PNG格式无损,因此特别推荐用于应用界面中使用的图像。

Also, there's an SVGKit that according to many devs is buggy so use it at your own risk. 此外,有一个SVGKit ,根据许多开发人员的错误,所以使用它需要您自担风险。

SVG is an XML-based vector and in Xcode you can also use vectors but using a PDF format and follow this tutorial. SVG是一个基于XML的矢量,在Xcode中你也可以使用矢量但是使用PDF格式并遵循教程。

In a nutshell: 简而言之:

  1. Generate PDFs With the @1x Asset (During compile time it will generate @2 and @3) 使用@ 1x资产生成PDF(在编译期间,它将生成@ 2和@ 3)
  2. Set the Scale Factors to Single Vector: 将比例因子设置为单个矢量:

在此输入图像描述

  1. Drag and Drop Your PDF Into the All, Universal Section 将PDF拖放到全部通用部分
  2. Refer to Your Image by Its Name, Like for any PNG File 按名称引用您的图像,与任何PNG文件一样

     [UIImage imageNamed:@”Home”] 

Also, stackoverflow (perhaps) related answer 另外,stackoverflow(也许) 相关的答案

To convert PNG to SVG (Which is a very sensible and valid thing to do) you need an app or library called a "tracer". 要将PNG转换为SVG(这是一个非常明智和有效的事情),您需要一个名为“tracer”的应用程序或库。 It will trace the outlines of shapes, gradients, etc and convert them into vector representations. 它将跟踪形状,渐变等的轮廓并将它们转换为矢量表示。

For simple cases, this is easy for a computer to do; 对于简单的情况,这对于计算机来说很容易; for complex cases (eg gradients), this is a very hard AI / Computer-Vision problem and you'd need to spend $$$ on high-end coding solutions and/or find PhD-level research that solves the problem! 对于复杂的情况(例如渐变),这是一个非常困难的AI /计算机视觉问题,你需要在高端编码解决方案上花费$$$和/或找到解决问题的博士级研究!

A free tracer that does a very good job is built-in to Inkscape (open-source) - google "inkscape trace" for tutorials on how to use it, and to generate a .svg file that you can use. 一个非常好的免费跟踪器内置于Inkscape(开源) - 谷歌“inkscape跟踪”,用于有关如何使用它的教程,以及生成可以使用的.svg文件。 This is a manual process. 这是一个手动过程。

To use this in-app, you need to find a tracing library for iOS. 要在应用程序中使用此功能,您需要找到适用于iOS的跟踪库。 The libraries that Inkscape uses are all open-source, so you could try converting them to iOS - they're written in C, so it could be quite easy. Inkscape使用的库都是开源的,因此您可以尝试将它们转换为iOS - 它们是用C语言编写的,因此它可能非常简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM