Skip to Content
Swift学习手册
book

Swift学习手册

by Jon Manning, Paris Buttfield-Addison, Tim Nugent
February 2017
Beginner to intermediate
472 pages
9h 53m
Chinese
China Electric Power Press Ltd.
Content preview from Swift学习手册
164
6
//
转换成
PNG
图像
return bitmapRepresentation?
.representationUsingType(.NSPNGFileType, properties: [:])
}
这个方法的作用是准备一个图像,然后返回一个包含
PNG
图像的
NSData
对象。
为了构建图像,我们先新建一个
NSImage
对象,传入想要的图像尺寸。然后,调用
lockFocus
方法,告诉绘制系统,我们准备开始绘制这个新图像了
接下来,开始绘制。首先,使用白色背景填充整个图像,然后检查有没有附件。如果有,
则在画布上半部分绘制附件的缩略图,在下半部分绘制笔记的文本;如果没有附件,
则只绘制文本。
绘制完毕后,创建一个
NSBitmapImageRep
对象,通过它把图像转换成位图格式,例
PNG
。这一步是必须的,因为
NSImage
可以轻易转换成矢量格式,如
PDF
;我们
要明确想对图像做什么。
得到位图表述后,调用
unlockFocus
方法,告诉绘制系统,对图像的处理结束了。
如果调用了
lockFocus
,就必须调用
unlockFocus
。如
若不然,会出现各种问题,因
为绘制系统不知道你结束绘制图像的内容了。
最后,让位图表述提供图像的
PNG
版本,返回一个
NSData
对象。
现在,文档能生成表示自己的缩略图了,我们可以在文档的快速查看中预览缩略图了。
3.
把下述代码添加到
fileWrapperOfType
方法中:
override func fileWrapperOfType(typeName: ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

PyTorch深度学习

PyTorch深度学习

Posts & Telecom Press, Vishnu Subramanian
深入淺出代數

深入淺出代數

Tracey Pilone, Dan Pilone
PHP学习手册

PHP学习手册

David Sklar
C语言核心技术(原书第2版)

C语言核心技术(原书第2版)

Peter Prinz, Tony Crawford

Publisher Resources

ISBN: 9787519803223