矢量图像如何在Xcode(即pdf文件)中工作?

时间:2022-11-21 10:27:40

How does vector support work in Xcode 6?

向量支持如何在Xcode 6中工作?

When I try resizing an image, it looks jagged, what gives?

当我尝试调整一个图像的大小时,它看起来参差不齐,是什么造成的呢?

5 个解决方案

#1


320  

How to use vectors in Xcode (7 and 6.3+):

  1. Save an image as a .pdf file at the proper @1x size (e.g. 44x44 for a toolbar button).
  2. 以适当的@1x大小将图像保存为.pdf文件(例如工具栏按钮的44x44)。
  3. In your Images.xcassets file, create a new Image Set.
  4. 在你的图片。xcassets文件,创建一个新的映像集。
  5. In the Attributes Inspector, set the Scale Factors to Single Vector.
  6. 在属性检查器中,将比例因子设置为单个向量。
  7. Drag and drop your pdf file into the All, Universal section.
  8. 拖放您的pdf文件到全通用部分。
  9. You can now refer to your image by its name, just as you would any .png file.

    您现在可以通过它的名称引用您的映像,就像您可以引用任何.png文件一样。

    UIImage(named: "myImage")

How to use vectors in older versions of Xcode (6.0 - 6.2):

  • Follow the steps above, except for step 3, set Types to Vectors.
  • 按照上面的步骤,除了步骤3,将类型设置为向量。

How vectors work in Xcode

Vector support is confusing in Xcode, because when most people think of vectors, they think of images that can scale up and down and still look good. However, Xcode 6 and 7 don't have full vector support for iOS, so things work a little differently.

向量支持在Xcode中是令人困惑的,因为当大多数人想到向量时,他们想到的是可以放大和缩小的图像,并且看起来仍然很好。然而,Xcode 6和7对iOS没有完全的矢量支持,所以事情有点不同。

The vector system is really simple. It takes your .pdf image, and creates @1x.png, @2x.png, and @3x.png assets at build time. (You can use a tool to examine the contents of Assets.car to verify this.)

向量系统非常简单。它需要你的。pdf图像,并创建@1x。png、@2x。png,@3x。构建时的png资产。(您可以使用工具检查资产的内容。汽车来验证这一点。)

For example, assume you are given foo.pdf which is a 44x44 vector asset. At build time it will generate the following files:

例如,假设您有foo。pdf是一个44x44矢量资产。在构建时,它将生成以下文件:

  • foo@1x.png at 44x44
  • foo@1x。png在44 x44
  • foo@2x.png at 88x88
  • foo@2x。png 88 x88
  • foo@3x.png at 132x132
  • foo@3x。png 132 x132

This works the same for any sized image. For example, if you have bar.pdf which is 100x100, you will get:

这对任何大小的图像都是一样的。例如,如果你有bar。pdf是100x100,你会得到:

  • bar@1x.png at 100x100
  • bar@1x。png 100 x100
  • bar@2x.png at 200x200
  • bar@2x。png 200 x200型
  • bar@3x.png at 300x300
  • bar@3x。png 300 x300

Implications:

  • You cannot choose a new size for the image; it will only look good if you keep it at the 44x44 size. The reason is that full vector support is not implemented. The only thing these vectors do is save you the time of saving your image assets. If you have a tool (e.g. a Photoshop script) that already makes this a one-step process, the only thing you will gain by using pdf vectors is future-proof support (e.g. if in iOS 9 Apple begins to require @4x assets, these will just work), and you will have fewer files to maintain.
  • 不能为图像选择新的大小;如果你把它保持在44×44的尺寸,它才会好看。原因是没有实现完全矢量支持。这些向量所做的唯一的事情是为您节省映像资源的时间。如果你有一个工具(如Photoshop脚本),已经使这一步的过程,你将获得唯一通过使用pdf向量是不会过时的技术支持(如:如果苹果iOS 9开始要求@4x资产,这些只会工作),你将有更少的文件维护。
  • You should ask for all your assets at the @1x size, saved as PDF files. Among other things, this will allow UIImageViews to have the correct intrinsic content size.
  • 您应该请求以@1x大小保存为PDF文件的所有资产。在其他方面,这将允许UIImageViews具有正确的内在内容大小。

Why it (probably) works this way:

  • This makes it backwards compatible with previous iOS versions.
  • 这使得它与以前的iOS版本向后兼容。
  • Resizing vectors may be a computational intensive task at runtime; by implementing it this way, there are no performance hits.
  • 在运行时调整矢量大小可能是一项计算密集型的任务;通过这种方式实现,就没有性能的影响。

#2


15  

In Xcode 8, you can still add a pdf, create a new Image Set, and in the Attributes Inspector, set the Scales to Single Scale option. 矢量图像如何在Xcode(即pdf文件)中工作?

在Xcode 8中,您仍然可以添加一个pdf,创建一个新的映像集,在属性检查器中,将Scale设置为单个Scale选项。

#3


13  

This is a supplement to the excellent answer by @Senseful.

这是@Senseful的优秀回答的补充。

How to make vector images in .pdf format

I will tell how to do this in Inkscape since it is free and open source but other programs should be similar.

我将告诉您如何在Inkscape中实现这一点,因为它是免费的和开源的,但是其他程序应该类似。

In Inkscape:

之:

  1. Create a new project.
  2. 创建一个新项目。
  3. Go to File > Document Properties and set the custom page size to whatever your @1x size is (44x44, 100x100, etc) with the units in px.
  4. 打开>文件属性,并将自定义页面大小设置为@1x大小(44x44, 100x100,等等)。
  5. Make your artwork.
  6. 让你的艺术作品。
  7. Go to File > Save As... > Printable Document Format (*.pdf) > Save > OK. (Alternatively, you could go to Print > Print to File > Output format: PDF > Print but there are not as many options.)
  8. 到文件>保存为…>可打印文档格式(*.pdf) >保存> OK。(也可以选择打印>打印到文件>输出格式:PDF >打印,但没有那么多的选项)

Notes:

注:

  • As is mentioned in the accepted answer, you cannot resize your image because Xcode still produces the rasterized images at build time. If you need to resize your image you should make a new .pdf file with a different size.
  • 正如在已接受的答案中提到的,您不能调整映像的大小,因为Xcode仍然在构建时生成栅格化映像。如果您需要调整图像大小,您应该创建一个不同大小的新的.pdf文件。
  • If you already have an .svg image that is the wrong page size, do the following:

    如果您已经有了一个.svg图像,但是页面大小不对,请执行以下操作:

    1. Change the page size (Inkscape > File > Document Properties)
    2. 更改页面大小(Inkscape >文件>文档属性)
    3. Select all objects (Ctrl+A) on the work space and resize them to fit in the new page size. (Hold down Ctrl to keep aspect size.)
    4. 选择工作空间中的所有对象(Ctrl+A),并调整它们的大小以适应新的页面大小。(按住Ctrl键保持尺寸。)
  • To convert an .svg file into a .pdf you can also find online utilities to do the job for you. Here is one example from this answer. This has the benefit of allowing you to set the .pdf size easily.

    要将.svg文件转换为.pdf文件,还可以找到在线实用程序为您完成这项工作。这是这个答案中的一个例子。这允许您轻松设置.pdf大小。

Further reading

#4


2  

For those who still not updated, there were changes in Xcode 9 (iOS 11).

对于那些还没有更新的人,Xcode 9 (ios11)有一些变化。

What’s new in Cocoa Touch (WWDC 2017 Session 201) (@32:55)https://developer.apple.com/videos/play/wwdc2017/201/

Cocoa Touch有什么新功能(WWDC 2017届201)(@32:55)(https://developer.apple.com/videos/play/wwdc2017/ /)

In few words, Asset Catalog now includes the new checkbox in Attributes Inspector named “Preserve Vector Data”. When checked, PDF data will be included in the compiled binary, increasing its size of course. But it gives a chance for iOS to scale the vector data in both directions and provide nice images.(With its own difficulties). For iOS below 11, old scaling mechanisms described in answers upwards is used.

简而言之,资产目录现在包括属性检查器中的新复选框,名为“保留向量数据”。当检查时,PDF数据将包含在已编译的二进制文件中,当然会增加它的大小。但它为iOS提供了一个在两个方向上扩展矢量数据并提供良好图像的机会。(有自己的困难)。对于11以下的iOS,使用了回答中描述的旧的缩放机制。

#5


0  

You can use normal PDF files inside your project as Vector images and render images of any size using this extension. This way is way better because iOS will not generate .PNG images out of your PDF files, plus you can render you images with any size you want:

您可以在项目中使用普通的PDF文件作为矢量图像,并使用此扩展来呈现任意大小的图像。这种方式更好,因为iOS不会从PDF文件中生成.PNG图像,而且你可以呈现任意大小的图像:

extension UIImage {static func fromPDF(filename: String, size: CGSize) -> UIImage? {    guard let path = Bundle.main.path(forResource: filename, ofType: "pdf") else { return nil }    let url = URL(fileURLWithPath: path)    guard let document = CGPDFDocument(url as CFURL) else { return nil }    guard let page = document.page(at: 1) else { return nil }    let imageRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)    if #available(iOS 10.0, *) {        let renderer = UIGraphicsImageRenderer(size: size)        let img = renderer.image { ctx in            UIColor.white.withAlphaComponent(0).set()            ctx.fill(imageRect)            ctx.cgContext.translateBy(x: 0, y: size.height)            ctx.cgContext.scaleBy(x: 1.0, y: -1.0)            ctx.cgContext.concatenate(page.getDrawingTransform(.artBox, rect: imageRect, rotate: 0, preserveAspectRatio: true))            ctx.cgContext.drawPDFPage(page);        }        return img    } else {        // Fallback on earlier versions        UIGraphicsBeginImageContextWithOptions(size, false, 2.0)        if let context = UIGraphicsGetCurrentContext() {            context.interpolationQuality = .high            context.setAllowsAntialiasing(true)            context.setShouldAntialias(true)            context.setFillColor(red: 1, green: 1, blue: 1, alpha: 0)            context.fill(imageRect)            context.saveGState()            context.translateBy(x: 0.0, y: size.height)            context.scaleBy(x: 1.0, y: -1.0)            context.concatenate(page.getDrawingTransform(.cropBox, rect: imageRect, rotate: 0, preserveAspectRatio: true))            context.drawPDFPage(page)            let image = UIGraphicsGetImageFromCurrentImageContext()            UIGraphicsEndImageContext()            return image        }        return nil    }}

}

}

#1


320  

How to use vectors in Xcode (7 and 6.3+):

  1. Save an image as a .pdf file at the proper @1x size (e.g. 44x44 for a toolbar button).
  2. 以适当的@1x大小将图像保存为.pdf文件(例如工具栏按钮的44x44)。
  3. In your Images.xcassets file, create a new Image Set.
  4. 在你的图片。xcassets文件,创建一个新的映像集。
  5. In the Attributes Inspector, set the Scale Factors to Single Vector.
  6. 在属性检查器中,将比例因子设置为单个向量。
  7. Drag and drop your pdf file into the All, Universal section.
  8. 拖放您的pdf文件到全通用部分。
  9. You can now refer to your image by its name, just as you would any .png file.

    您现在可以通过它的名称引用您的映像,就像您可以引用任何.png文件一样。

    UIImage(named: "myImage")

How to use vectors in older versions of Xcode (6.0 - 6.2):

  • Follow the steps above, except for step 3, set Types to Vectors.
  • 按照上面的步骤,除了步骤3,将类型设置为向量。

How vectors work in Xcode

Vector support is confusing in Xcode, because when most people think of vectors, they think of images that can scale up and down and still look good. However, Xcode 6 and 7 don't have full vector support for iOS, so things work a little differently.

向量支持在Xcode中是令人困惑的,因为当大多数人想到向量时,他们想到的是可以放大和缩小的图像,并且看起来仍然很好。然而,Xcode 6和7对iOS没有完全的矢量支持,所以事情有点不同。

The vector system is really simple. It takes your .pdf image, and creates @1x.png, @2x.png, and @3x.png assets at build time. (You can use a tool to examine the contents of Assets.car to verify this.)

向量系统非常简单。它需要你的。pdf图像,并创建@1x。png、@2x。png,@3x。构建时的png资产。(您可以使用工具检查资产的内容。汽车来验证这一点。)

For example, assume you are given foo.pdf which is a 44x44 vector asset. At build time it will generate the following files:

例如,假设您有foo。pdf是一个44x44矢量资产。在构建时,它将生成以下文件:

  • foo@1x.png at 44x44
  • foo@1x。png在44 x44
  • foo@2x.png at 88x88
  • foo@2x。png 88 x88
  • foo@3x.png at 132x132
  • foo@3x。png 132 x132

This works the same for any sized image. For example, if you have bar.pdf which is 100x100, you will get:

这对任何大小的图像都是一样的。例如,如果你有bar。pdf是100x100,你会得到:

  • bar@1x.png at 100x100
  • bar@1x。png 100 x100
  • bar@2x.png at 200x200
  • bar@2x。png 200 x200型
  • bar@3x.png at 300x300
  • bar@3x。png 300 x300

Implications:

  • You cannot choose a new size for the image; it will only look good if you keep it at the 44x44 size. The reason is that full vector support is not implemented. The only thing these vectors do is save you the time of saving your image assets. If you have a tool (e.g. a Photoshop script) that already makes this a one-step process, the only thing you will gain by using pdf vectors is future-proof support (e.g. if in iOS 9 Apple begins to require @4x assets, these will just work), and you will have fewer files to maintain.
  • 不能为图像选择新的大小;如果你把它保持在44×44的尺寸,它才会好看。原因是没有实现完全矢量支持。这些向量所做的唯一的事情是为您节省映像资源的时间。如果你有一个工具(如Photoshop脚本),已经使这一步的过程,你将获得唯一通过使用pdf向量是不会过时的技术支持(如:如果苹果iOS 9开始要求@4x资产,这些只会工作),你将有更少的文件维护。
  • You should ask for all your assets at the @1x size, saved as PDF files. Among other things, this will allow UIImageViews to have the correct intrinsic content size.
  • 您应该请求以@1x大小保存为PDF文件的所有资产。在其他方面,这将允许UIImageViews具有正确的内在内容大小。

Why it (probably) works this way:

  • This makes it backwards compatible with previous iOS versions.
  • 这使得它与以前的iOS版本向后兼容。
  • Resizing vectors may be a computational intensive task at runtime; by implementing it this way, there are no performance hits.
  • 在运行时调整矢量大小可能是一项计算密集型的任务;通过这种方式实现,就没有性能的影响。

#2


15  

In Xcode 8, you can still add a pdf, create a new Image Set, and in the Attributes Inspector, set the Scales to Single Scale option. 矢量图像如何在Xcode(即pdf文件)中工作?

在Xcode 8中,您仍然可以添加一个pdf,创建一个新的映像集,在属性检查器中,将Scale设置为单个Scale选项。

#3


13  

This is a supplement to the excellent answer by @Senseful.

这是@Senseful的优秀回答的补充。

How to make vector images in .pdf format

I will tell how to do this in Inkscape since it is free and open source but other programs should be similar.

我将告诉您如何在Inkscape中实现这一点,因为它是免费的和开源的,但是其他程序应该类似。

In Inkscape:

之:

  1. Create a new project.
  2. 创建一个新项目。
  3. Go to File > Document Properties and set the custom page size to whatever your @1x size is (44x44, 100x100, etc) with the units in px.
  4. 打开>文件属性,并将自定义页面大小设置为@1x大小(44x44, 100x100,等等)。
  5. Make your artwork.
  6. 让你的艺术作品。
  7. Go to File > Save As... > Printable Document Format (*.pdf) > Save > OK. (Alternatively, you could go to Print > Print to File > Output format: PDF > Print but there are not as many options.)
  8. 到文件>保存为…>可打印文档格式(*.pdf) >保存> OK。(也可以选择打印>打印到文件>输出格式:PDF >打印,但没有那么多的选项)

Notes:

注:

  • As is mentioned in the accepted answer, you cannot resize your image because Xcode still produces the rasterized images at build time. If you need to resize your image you should make a new .pdf file with a different size.
  • 正如在已接受的答案中提到的,您不能调整映像的大小,因为Xcode仍然在构建时生成栅格化映像。如果您需要调整图像大小,您应该创建一个不同大小的新的.pdf文件。
  • If you already have an .svg image that is the wrong page size, do the following:

    如果您已经有了一个.svg图像,但是页面大小不对,请执行以下操作:

    1. Change the page size (Inkscape > File > Document Properties)
    2. 更改页面大小(Inkscape >文件>文档属性)
    3. Select all objects (Ctrl+A) on the work space and resize them to fit in the new page size. (Hold down Ctrl to keep aspect size.)
    4. 选择工作空间中的所有对象(Ctrl+A),并调整它们的大小以适应新的页面大小。(按住Ctrl键保持尺寸。)
  • To convert an .svg file into a .pdf you can also find online utilities to do the job for you. Here is one example from this answer. This has the benefit of allowing you to set the .pdf size easily.

    要将.svg文件转换为.pdf文件,还可以找到在线实用程序为您完成这项工作。这是这个答案中的一个例子。这允许您轻松设置.pdf大小。

Further reading

#4


2  

For those who still not updated, there were changes in Xcode 9 (iOS 11).

对于那些还没有更新的人,Xcode 9 (ios11)有一些变化。

What’s new in Cocoa Touch (WWDC 2017 Session 201) (@32:55)https://developer.apple.com/videos/play/wwdc2017/201/

Cocoa Touch有什么新功能(WWDC 2017届201)(@32:55)(https://developer.apple.com/videos/play/wwdc2017/ /)

In few words, Asset Catalog now includes the new checkbox in Attributes Inspector named “Preserve Vector Data”. When checked, PDF data will be included in the compiled binary, increasing its size of course. But it gives a chance for iOS to scale the vector data in both directions and provide nice images.(With its own difficulties). For iOS below 11, old scaling mechanisms described in answers upwards is used.

简而言之,资产目录现在包括属性检查器中的新复选框,名为“保留向量数据”。当检查时,PDF数据将包含在已编译的二进制文件中,当然会增加它的大小。但它为iOS提供了一个在两个方向上扩展矢量数据并提供良好图像的机会。(有自己的困难)。对于11以下的iOS,使用了回答中描述的旧的缩放机制。

#5


0  

You can use normal PDF files inside your project as Vector images and render images of any size using this extension. This way is way better because iOS will not generate .PNG images out of your PDF files, plus you can render you images with any size you want:

您可以在项目中使用普通的PDF文件作为矢量图像,并使用此扩展来呈现任意大小的图像。这种方式更好,因为iOS不会从PDF文件中生成.PNG图像,而且你可以呈现任意大小的图像:

extension UIImage {static func fromPDF(filename: String, size: CGSize) -> UIImage? {    guard let path = Bundle.main.path(forResource: filename, ofType: "pdf") else { return nil }    let url = URL(fileURLWithPath: path)    guard let document = CGPDFDocument(url as CFURL) else { return nil }    guard let page = document.page(at: 1) else { return nil }    let imageRect = CGRect(x: 0, y: 0, width: size.width, height: size.height)    if #available(iOS 10.0, *) {        let renderer = UIGraphicsImageRenderer(size: size)        let img = renderer.image { ctx in            UIColor.white.withAlphaComponent(0).set()            ctx.fill(imageRect)            ctx.cgContext.translateBy(x: 0, y: size.height)            ctx.cgContext.scaleBy(x: 1.0, y: -1.0)            ctx.cgContext.concatenate(page.getDrawingTransform(.artBox, rect: imageRect, rotate: 0, preserveAspectRatio: true))            ctx.cgContext.drawPDFPage(page);        }        return img    } else {        // Fallback on earlier versions        UIGraphicsBeginImageContextWithOptions(size, false, 2.0)        if let context = UIGraphicsGetCurrentContext() {            context.interpolationQuality = .high            context.setAllowsAntialiasing(true)            context.setShouldAntialias(true)            context.setFillColor(red: 1, green: 1, blue: 1, alpha: 0)            context.fill(imageRect)            context.saveGState()            context.translateBy(x: 0.0, y: size.height)            context.scaleBy(x: 1.0, y: -1.0)            context.concatenate(page.getDrawingTransform(.cropBox, rect: imageRect, rotate: 0, preserveAspectRatio: true))            context.drawPDFPage(page)            let image = UIGraphicsGetImageFromCurrentImageContext()            UIGraphicsEndImageContext()            return image        }        return nil    }}

}

}