<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.8.5">Jekyll</generator><link href="https://leafduo.com/atom.xml" rel="self" type="application/atom+xml" /><link href="https://leafduo.com/" rel="alternate" type="text/html" /><updated>2019-06-03T05:14:35+08:00</updated><id>https://leafduo.com/atom.xml</id><title type="html">双叶实验室</title><subtitle>Currently write Go code, formerly iOS developer</subtitle><author><name>Zuyang Kou</name></author><entry><title type="html">Live Reload 工具 modd</title><link href="https://leafduo.com/articles/2019/03/24/modd/" rel="alternate" type="text/html" title="Live Reload 工具 modd" /><published>2019-03-24T00:00:00+08:00</published><updated>2019-03-24T00:00:00+08:00</updated><id>https://leafduo.com/articles/2019/03/24/modd</id><content type="html" xml:base="https://leafduo.com/articles/2019/03/24/modd/">&lt;p&gt;最近开始用 go 写后端程序，每次 &lt;code&gt;go run&lt;/code&gt; 感觉累得要死，非常需要一个 live reload 的工具。一些框架会自带自动重启功能，比如 &lt;a href=&quot;https://gobuffalo.io/en&quot;&gt;Buffalo&lt;/a&gt;，不巧的是我们没有用框架，所以就需要找一些专门的 live reload 的工具了。&lt;/p&gt;

&lt;p&gt;社区比较流行的是 &lt;a href=&quot;https://github.com/oxequa/realize&quot;&gt;realize&lt;/a&gt; 这个工具，尝试用了一下，发现并不支持 go 1.11 引入的 module 机制&lt;a href=&quot;https://github.com/oxequa/realize/issues/217&quot;&gt;[1]&lt;/a&gt;。并且似乎作者似乎已经放弃了这个项目，上一次提交已经是半年前了，对&lt;a href=&quot;https://github.com/oxequa/realize/pull/218&quot;&gt;修复这个问题的 PR&lt;/a&gt; 也没有表态。&lt;/p&gt;

&lt;p&gt;放弃了 realize 之后，目光转向了另一个工具 &lt;a href=&quot;https://github.com/cosmtrek/air&quot;&gt;air&lt;/a&gt;。这个工具基本开箱即用，安装之后在项目目录里执行 &lt;code&gt;air&lt;/code&gt;就可以了之后就可以看到项目跑了起来，当然，没有配置的前提是项目要符合一定的规则，一般的 go 项目应该都问题不大。&lt;/p&gt;

&lt;p&gt;不过用了一段时间之后发现，air 满足不了一些需求。例如说 air 不支持运行程序的时候带参数，自动执行测试等可能也比较复杂。幸好，我又找到了本篇文章的主角 &lt;a href=&quot;https://github.com/cortesi/modd&quot;&gt;modd&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;Modd 是一个响应文件变化执行指定的命令的工具。它并没有限制我们执行什么命令，是一个相当通用的工具，可以用于各种语言、各种项目的热重启。当然，通用就决定了它并不能对项目做出过多的假设，需要我们事先进行配置才能正确地完成 live reload。&lt;/p&gt;

&lt;p&gt;Modd 在执行时会检查名叫 &lt;code&gt;modd.conf&lt;/code&gt; 的配置文件，例如：&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;**/*.go {
    prep: go test ./...
    daemon: go run main.go
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;之后再执行 &lt;code&gt;modd&lt;/code&gt; 就可以完成监控文件变化并热重启了。示例可以在 &lt;a href=&quot;https://github.com/leafduo/modd-example&quot;&gt;https://github.com/leafduo/modd-example&lt;/a&gt; 找到。&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/modd-example.png&quot; alt=&quot;modd-example&quot;&gt;
我们来仔细看一下这个配置文件，首先指定了需要监控的文件的列表，这里是 &lt;code&gt;**/*.go&lt;/code&gt;，也就是所有子孙目录下的 go 文件。花括号中的内容是文件发生变化时执行的命令。&lt;code&gt;prep&lt;/code&gt; 后面的命令是会执行完成的类型，例如编译、单元测试、代码生成等；&lt;code&gt;damon&lt;/code&gt; 后面的命令是会一直执行、不会退出的命令，例如 web 服务器等。按照这个配置文件，运行 &lt;code&gt;modd&lt;/code&gt; 之后，每次修改文件之后，modd 就会自动执行测试，并重新运行程序。&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;protos/**/* **/*.go !pb/**/* {
    prep: go generate ./...
    prep: go test ./...
    daemon: go run main.go
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;再来看一个复杂一点的例子，这个例子中，我在项目中有一些 protobuf 的描述文件，我已经在代码中写好了生成相应的 go 文件的指令，只需要运行 &lt;code&gt;go generate&lt;/code&gt; 就可以生成 &lt;code&gt;.pb.go&lt;/code&gt; 文件在程序中使用了。 监控的文件列表中，增加了 &lt;code&gt;protos/**/*&lt;/code&gt;，这里面包含了 protobuf 描述文件；还有一项是 &lt;code&gt;!pb/**/*&lt;/code&gt;，这里排除了 &lt;code&gt;pb&lt;/code&gt; 文件夹下的所有文件，避免自动生成的文件再次触发 reload。&lt;/p&gt;

&lt;p&gt;总体来说，modd 是非常顺手的，配置灵活，功能完善，开发调试必备良品。&lt;/p&gt;</content><author><name>Zuyang Kou</name></author><summary type="html">最近开始用 go 写后端程序，每次 go run 感觉累得要死，非常需要一个 live reload 的工具。一些框架会自带自动重启功能，比如 Buffalo，不巧的是我们没有用框架，所以就需要找一些专门的 live reload 的工具了。</summary></entry><entry><title type="html">__attribute__((objc_designated_initializer))</title><link href="https://leafduo.com/articles/2014/03/20/objc-designated-initializer/" rel="alternate" type="text/html" title="__attribute__((objc_designated_initializer))" /><published>2014-03-20T00:00:00+08:00</published><updated>2014-03-20T00:00:00+08:00</updated><id>https://leafduo.com/articles/2014/03/20/objc-designated-initializer</id><content type="html" xml:base="https://leafduo.com/articles/2014/03/20/objc-designated-initializer/">&lt;p&gt;指定初始化器（designated initializer）是 Objective-C 中的一个重要的概念，但是很可惜的是，很多开发者（不知道为什么）并没能正确遵守关于指定初始化器的一些惯例。
之前，我们只能通过 code review 之类的方法来找出、修正这些问题；现在，clang 为我们提供了编译器级别的支持，能够找出不遵守管理的地方并给出警告，我们需要做的是标记出哪个初始化器是指定初始化器，例如：&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objc&quot; data-lang=&quot;objc&quot;&gt;&lt;span class=&quot;k&quot;&gt;@interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FTBObject&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;instancetype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;init&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__attribute__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;objc_designated_initializer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;@end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;通过在接口定义中指定指定初始化器，不仅编译器能够给出相关的警告，而且需要继承该类的开发者也可以不参考文档就知道哪个初始化器是指定初始化器。&lt;/p&gt;

&lt;p&gt;目前，clang 可以提供一系列警告，例如非指定初始化器没有调用其他初始化器、指定初始化器没有调用父类的指定初始化器、指定初始化器调用了非指定初始化器、子类没有复写父类的指定初始化器等。
具体的例子可以看这个&lt;a href=&quot;https://llvm.org/svn/llvm-project/cfe/trunk/test/SemaObjC/attr-designated-init.m&quot;&gt;测试用例&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;不幸的是，目前系统库的接口还没有这个属性，也就是说只有继承自己的代码和标记了这个指令的第三方库中的类才有用。&lt;/p&gt;</content><author><name>Zuyang Kou</name></author><summary type="html">指定初始化器（designated initializer）是 Objective-C 中的一个重要的概念，但是很可惜的是，很多开发者（不知道为什么）并没能正确遵守关于指定初始化器的一些惯例。 之前，我们只能通过 code review 之类的方法来找出、修正这些问题；现在，clang 为我们提供了编译器级别的支持，能够找出不遵守管理的地方并给出警告，我们需要做的是标记出哪个初始化器是指定初始化器，例如：</summary></entry><entry><title type="html">iOS 7 SDK Mirgration Cheat Sheet</title><link href="https://leafduo.com/articles/2013/10/17/ios7-mirgation-cheat-sheet/" rel="alternate" type="text/html" title="iOS 7 SDK Mirgration Cheat Sheet" /><published>2013-10-17T00:00:00+08:00</published><updated>2013-10-17T00:00:00+08:00</updated><id>https://leafduo.com/articles/2013/10/17/ios7-mirgation-cheat-sheet</id><content type="html" xml:base="https://leafduo.com/articles/2013/10/17/ios7-mirgation-cheat-sheet/">&lt;p&gt;最近搞了一点 iOS 7 SDK 的适配，来几个作弊条&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;UITableViewCell 默认有了一个白色背景，我们来去掉它&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objc&quot; data-lang=&quot;objc&quot;&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;backgroundColor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UIColor&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;clearColor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;原本 &lt;code&gt;UITableView&lt;/code&gt; 是它的 &lt;code&gt;contentView&lt;/code&gt; 的 &lt;code&gt;superView&lt;/code&gt;，现在中间多了一个 &lt;code&gt;UITableViewCellScrollView&lt;/code&gt;，而且这个 &lt;code&gt;UITableViewCellScrollView&lt;/code&gt; 默认 &lt;code&gt;clipsToBounds = YES&lt;/code&gt;。这就导致很多（没有正确编写的） UITableViewCell 被截断。先来个 quick fix：&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objc&quot; data-lang=&quot;objc&quot;&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;contentView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;superView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clipsToBounds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;YES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;不过这仅仅是个 &lt;em&gt;quick fix&lt;/em&gt;，真正想修正请&lt;em&gt;正确计算 frame&lt;/em&gt;，不要偷懒！&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;UISearchBar&lt;/code&gt; 多了一个 &lt;code&gt;searchBarStyle&lt;/code&gt; 属性，把它设置成 &lt;code&gt;UISearchBarStyleMinimal&lt;/code&gt; 然后自己设置背景色：&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objective-c&quot; data-lang=&quot;objective-c&quot;&gt;if ([_searchBar respondsToSelector:@selector(setSearchBarStyle:)]) {
    _searchBar.searchBarStyle = UISearchBarStyleMinimal;
    _searchBar.backgroundColor = [UIColor whiteColor];
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;UISearchDisplayController&lt;/code&gt; 带出来的显示搜索结果的 table view 没有背景，会跟后面的 view 叠起来：&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objc&quot; data-lang=&quot;objc&quot;&gt;&lt;span class=&quot;k&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;searchDisplayController&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UISearchDisplayController&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;controller&lt;/span&gt;
   &lt;span class=&quot;nf&quot;&gt;didLoadSearchResultsTableView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UITableView&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;tableView&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;tableView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;backgroundColor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UIColor&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;whiteColor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;没有 UINavigationController 的视图中，处理 status bar 的方法：&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objc&quot; data-lang=&quot;objc&quot;&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;wantsFullScreenLayout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;YES&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;之后把里面的 view 都下移 20 points。&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;说实话这个我真没用到：&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objc&quot; data-lang=&quot;objc&quot;&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;respondsToSelector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;@selector&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;setEdgesForExtendedLayout&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:)])&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;edgesForExtendedLayout&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UIRectEdgeNone&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;因为我用了&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;```objc
self.navigationBar.translucent = NO;
```
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Apple 对许多属性的默认值做了修改，这应该是有它的意义的，大概是指导设计？我还没有想清楚。&lt;/p&gt;

&lt;p&gt;修改的过程中发现除了 Apple 改的那些默认值以外大部分都是历史问题，出来混的，总是要还的。&lt;/p&gt;</content><author><name>Zuyang Kou</name></author><summary type="html">最近搞了一点 iOS 7 SDK 的适配，来几个作弊条</summary></entry><entry><title type="html">iOS 7</title><link href="https://leafduo.com/articles/2013/09/28/ios7/" rel="alternate" type="text/html" title="iOS 7" /><published>2013-09-28T00:00:00+08:00</published><updated>2013-09-28T00:00:00+08:00</updated><id>https://leafduo.com/articles/2013/09/28/ios7</id><content type="html" xml:base="https://leafduo.com/articles/2013/09/28/ios7/">&lt;p&gt;译自 &lt;a href=&quot;http://nshipster.com/ios7/&quot;&gt;NSHispter&lt;/a&gt;。&lt;/p&gt;

&lt;p&gt;NDA 终于结束了，我们终于可以聊聊 iOS 7 里超赞的新 API 了。而且新 API 有&lt;em&gt;非常多&lt;/em&gt;。苹果在 WWDC Keynote 中说有“1500 个新 API”，。（尽管里面很大一部分只是把 &lt;code&gt;id&lt;/code&gt; 换成了 &lt;code&gt;instancetype&lt;/code&gt;，但是不管怎么说还是非常多）&lt;/p&gt;

&lt;p&gt;带着版本升级的激动心情，接下来的几周我们会深入讨论 iOS 7 的许多新特性，但是这周，我们来看下那些隐藏颇深的新特性：&lt;code&gt;NSData&lt;/code&gt; Base64 编码, &lt;code&gt;NSURLComponents&lt;/code&gt;, &lt;code&gt;NSProgress&lt;/code&gt;, &lt;code&gt;NSProcessInfo&lt;/code&gt; activities, &lt;code&gt;CIDetectorSmile&lt;/code&gt;, &lt;code&gt;CIDetectorEyeBlink&lt;/code&gt;, &lt;code&gt;SSReadingList&lt;/code&gt;, &lt;code&gt;AVCaptureMetaDataOutput&lt;/code&gt;, &lt;code&gt;AVSpeechSynthesizer&lt;/code&gt; 和 &lt;code&gt;MKDistanceFormatter&lt;/code&gt;。&lt;/p&gt;

&lt;hr&gt;

&lt;h2&gt;NSData (NSDataBase64Encoding)&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Base64&quot;&gt;Base64&lt;/a&gt; 是一种常用的把二进制编码成 ASCII 字符的手段。由于许多常用 web 技术只支持文本，并不支持二进制，所以 Base64 应用十分广泛。例如，CSS 可以用 &lt;a href=&quot;http://en.wikipedia.org/wiki/Data_URI_scheme&quot;&gt;inline &lt;code&gt;data://&lt;/code&gt; URIs&lt;/a&gt; 来内嵌图片，这种形式就经常使用 Base64 编码的文本。另一个例子是 &lt;a href=&quot;http://en.wikipedia.org/wiki/Basic_access_authentication&quot;&gt;Basic &lt;code&gt;Authentication&lt;/code&gt; headers&lt;/a&gt;，它使用 Base64 编码用户名/密码对，稍微比完全明文好一点吧。&lt;/p&gt;

&lt;p&gt;长久以来，这个重要的函数压根就没有，数以千计的开发者治好到论坛上随便抓一段代码贴过来就用。如此显而易见的缺失就像 iOS 5 之前的 JSON 一样恼人。&lt;/p&gt;

&lt;p&gt;但是现在！iOS 7 终于内置了 Base64：&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objective-c&quot; data-lang=&quot;objective-c&quot;&gt;NSString *string = @&quot;Lorem ipsum dolor sit amet.&quot;;
NSString *base64EncodedString = [[string dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];

NSLog(@&quot;%@&quot;, base64EncodedString); // @&quot;TG9yZW0gaXBzdW0gZG9sYXIgc2l0IGFtZXQu&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;NSURLComponents &amp;amp; NSCharacterSet (NSURLUtilities)&lt;/h2&gt;

&lt;p&gt;Foundation 框架里面有许多 URI 相关的功能。不过，很多功能是用 &lt;code&gt;NSString&lt;/code&gt; 实现的，因为 &lt;code&gt;NSURL&lt;/code&gt; 是不可改变的。&lt;/p&gt;

&lt;p&gt;&lt;code&gt;NSURLComponents&lt;/code&gt; 极大地改善了这种情况。把它当成是 &lt;code&gt;NSMutableURL&lt;/code&gt; 就好了：&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objective-c&quot; data-lang=&quot;objective-c&quot;&gt;NSURLComponents *components = [NSURLComponents componentsWithString:@&quot;http://nshipster.com&quot;];
components.path = @&quot;/iOS7&quot;;
components.query = @&quot;foo=bar&quot;;

NSLog(@&quot;%@&quot;, components.scheme); // @&quot;http&quot;
NSLog(@&quot;%@&quot;, [components URL]); // @&quot;http://nshipster.com/iOS7?foo=bar&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;URL components 的每个属性都有一个 &lt;code&gt;percentEncoded*&lt;/code&gt; 的变种（例如 &lt;code&gt;user&lt;/code&gt; 和 &lt;code&gt;percentEncodedUser&lt;/code&gt;），这些变种不会对 URI 中的特殊字符进行百分号编码。&lt;/p&gt;

&lt;p&gt;你说那些字符算是特殊字符？好吧，这跟字符处在 URL 的哪个部分有关。好消息是 iOS 7 中 &lt;code&gt;NSCharacterSet&lt;/code&gt; 增加了一个新的 category 来查询允许出现在 URL 中的字符。&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;+ (id)URLUserAllowedCharacterSet&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;+ (id)URLPasswordAllowedCharacterSet&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;+ (id)URLHostAllowedCharacterSet&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;+ (id)URLPathAllowedCharacterSet&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;+ (id)URLQueryAllowedCharacterSet&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;+ (id)URLFragmentAllowedCharacterSet&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;NSProgress&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;NSProgess&lt;/code&gt; 是一个有点难描述的类。它既是一个观察者，又是一个委托/协调者，扮演者报告和监控过程的角色。它在 OS X 上的系统级进程进行了整合，不过也可以用于面向用户的 UI。它可以指定暂停和取消的处理逻辑，让这些代码来真正地暂停和取消操作。&lt;/p&gt;

&lt;p&gt;任何具有完成状态和总量概念的东西都可以使用 &lt;code&gt;NSProgress&lt;/code&gt;，不管是写入文件的字节数、渲染的帧数，还是要从服务器上下载的文件数。&lt;/p&gt;

&lt;p&gt;&lt;code&gt;NSProgress&lt;/code&gt; 可以用本地化的方式来汇报总体进度：&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objective-c&quot; data-lang=&quot;objective-c&quot;&gt;NSProgress *progress = [NSProgress progressWithTotalUnitCount:100];
progress.completedUnitCount = 42;

NSLog(@&quot;%@&quot;, [progress localizedDescription]); // 42% completed
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;…或者可以给一个代码块来让操作完全停止：&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objective-c&quot; data-lang=&quot;objective-c&quot;&gt;NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 
                                         target:self 
                                       selector:@selector(incrementCompletedUnitCount:) userInfo:nil 
                                        repeats:YES];

progress.cancellationHandler = ^{
    [timer invalidate];
};

[progress cancel];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;NSProgess&lt;/code&gt; 在 Mac OS X 10.9 Mavericks 下很有用，但是在当前也还是一个包装工作量这个模式的一个很好的类。&lt;/p&gt;

&lt;h2&gt;NSArray -firstObject&lt;/h2&gt;

&lt;p&gt;欢呼吧！&lt;code&gt;NSArray&lt;/code&gt; 中避免 &lt;code&gt;NSRangeException&lt;/code&gt; 的 &lt;code&gt;-lastObject&lt;/code&gt; 终于有了对应的取第一个元素对应方法。（好吧，实际上这个方法在 iOS 4 的时候就已经成为私有 API 了，但是现在终于开放出来了）&lt;/p&gt;

&lt;p&gt;注意了！&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objective-c&quot; data-lang=&quot;objective-c&quot;&gt;NSArray *array = @[@1, @2, @3];

NSLog(@&quot;First Object: %@&quot;, [array firstObject]); // 1
NSLog(@&quot;Last Object: %@&quot;, [array lastObject]); // 3
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;爽！&lt;/p&gt;

&lt;h2&gt;CIDetectorSmile &amp;amp; CIDetectorEyeBlink&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;As a random aside, shouldn&amp;#39;t it be a cause for concern that the device most capable of taking embarrassing photos of ourselves is also the device most capable of distributing it to millions or people? Just a thought.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;iOS 5 以后，Core Image 在 &lt;code&gt;CIDetector&lt;/code&gt; 类中加入了面部识别和检测功能。如果检测照片中的人脸还不够炫的话，在 iOS 7 中我们甚至能检测笑脸和闭眼。*发抖*&lt;/p&gt;

&lt;p&gt;又一个免费 app 的点子，只保存笑脸照片的相机 app，其中的一段代码可能是这样的：&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objective-c&quot; data-lang=&quot;objective-c&quot;&gt;@import CoreImage;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objective-c&quot; data-lang=&quot;objective-c&quot;&gt;CIDetector *smileDetector = [CIDetector detectorOfType:CIDetectorSmile 
                                context:context 
                                options:@{CIDetectorTracking: @YES, 
                                          CIDetectorAccuracy: CIDetectorAccuracyLow}];
NSArray *features = [smileDetector featuresInImage:image];
if ([features count] &amp;gt; 0) {
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(didFinishWritingImage), features);
} else {
    self.label.text = @&quot;Say Cheese!&quot;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;AVCaptureMetaDataOutput&lt;/h2&gt;

&lt;p&gt;iOS 7 里在 &lt;code&gt;AVCaptureMetaDataOutput&lt;/code&gt; 中新增加了扫描 UPC、QR 码、普通条形码和其他变种的方法。只需要添加一个 &lt;code&gt;AVCaptureSession&lt;/code&gt; 的输出，实现相应的 &lt;code&gt;captureOutput:didOutputMetadataObjects:fromConnection:&lt;/code&gt; 方法：&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objective-c&quot; data-lang=&quot;objective-c&quot;&gt;@import AVFoundation;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objective-c&quot; data-lang=&quot;objective-c&quot;&gt;AVCaptureSession *session = [[AVCaptureSession alloc] init];
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
NSError *error = nil;

AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device 
                                                                    error:&amp;amp;error];
if (input) {
    [session addInput:input];
} else {
    NSLog(@&quot;Error: %@&quot;, error);
}

AVCaptureMetadataOutput *output = [[AVCaptureMetadataOutput alloc] init];
[output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
[session addOutput:output];

[session startRunning];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objective-c&quot; data-lang=&quot;objective-c&quot;&gt;#pragma mark - AVCaptureMetadataOutputObjectsDelegate

- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputMetadataObjects:(NSArray *)metadataObjects
       fromConnection:(AVCaptureConnection *)connection
{
    NSString *QRCode = nil;
    for (AVMetadataObject *metadata in metadataObjects) {
        if ([metadata.type isEqualToString:AVMetadataObjectTypeQRCode]) {
            // This will never happen; nobody has ever scanned a QR code... ever
            QRCode = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
            break;
        }
    }

    NSLog(@&quot;QR Code: %@&quot;, QRCode);
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;&lt;code&gt;AVFoundation&lt;/code&gt; 支持你听说过的所有条形码（大概还有一些没听过的）：&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;AVMetadataObjectTypeUPCECode&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;AVMetadataObjectTypeCode39Code&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;AVMetadataObjectTypeCode39Mod43Code&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;AVMetadataObjectTypeEAN13Code&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;AVMetadataObjectTypeEAN8Code&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;AVMetadataObjectTypeCode93Code&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;AVMetadataObjectTypeCode128Code&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;AVMetadataObjectTypePDF417Code&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;AVMetadataObjectTypeQRCode&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;AVMetadataObjectTypeAztecCode&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;就算没有其他用途，&lt;code&gt;AVCaptureMetaDataOutput&lt;/code&gt; 也使得在 iPhone 和 iPad 上创建一个 Passbook 条码读取器变得非常简单。&lt;/p&gt;

&lt;h2&gt;SSReadingList&lt;/h2&gt;

&lt;p&gt;即使真正会看保存在稍后阅读中的文章的人很少，可能只比&lt;a href=&quot;http://picturesofpeoplescanningqrcodes.tumblr.com&quot;&gt;扫描过 QR 码的人&lt;/a&gt;多一些。iOS 7 里新的 Safari Services framework 还是带来一个不错的功能：向 Safari 的阅读列表中添加文章。&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objective-c&quot; data-lang=&quot;objective-c&quot;&gt;@import SafariServices;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objective-c&quot; data-lang=&quot;objective-c&quot;&gt;NSURL *URL = [NSURL URLWithString:@&quot;http://nshipster.com/ios7&quot;];
[[SSReadingList defaultReadingList] addReadingListItemWithURL:URL
                                                        title:@&quot;NSHipster&quot;
                                                  previewText:@&quot;...&quot; 
                                                        error:nil];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;AVSpeechSynthesizer&lt;/h2&gt;

&lt;p&gt;语音合成（TTS）技术在 1960 年代后期出现以来，就成为了&lt;a href=&quot;http://nshipster.com/uiaccessibility/&quot;&gt;可用性&lt;/a&gt;和&lt;a href=&quot;http://nshipster.com/icloud/&quot;&gt;恶作剧爱好者&lt;/a&gt;的利器。&lt;/p&gt;

&lt;p&gt;iOS 7 把 Siri 的方便易用的&lt;a href=&quot;http://en.wikipedia.org/wiki/Speak_%26_Spell_%28toy%29&quot;&gt;Speak &amp;amp; Spell&lt;/a&gt; 功能带到了一个新的类 &lt;code&gt;AVSpeechSynthesizer&lt;/code&gt; 中：&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objective-c&quot; data-lang=&quot;objective-c&quot;&gt;AVSpeechSynthesizer *synthesizer = [[AVSpeechSynthesizer alloc] init];
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:@&quot;Hello, World!&quot;];
utterance.rate = AVSpeechUtteranceMinimumSpeechRate; // Tell it to me slowly
[synthesizer speakUtterance:utterance];
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;h2&gt;MKDistanceFormatter&lt;/h2&gt;

&lt;p&gt;最后，我们用 NSHipster 会喊出“终于来了！”的另一个类来结束我们的 iOS 7 的又新又炫的新 API 介绍，它就是：&lt;code&gt;MKDistanceFormatter&lt;/code&gt;。&lt;/p&gt;

&lt;p&gt;就像上面说的，&lt;code&gt;MKDistanceFormatter&lt;/code&gt; 提供了一种把公制/英制距离转换成本地化字符串的方法。&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objective-c&quot; data-lang=&quot;objective-c&quot;&gt;@import CoreLocation;
@import MapKit;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objective-c&quot; data-lang=&quot;objective-c&quot;&gt;CLLocation *sanFrancisco = [[CLLocation alloc] initWithLatitude:37.775 longitude:-122.4183333];
CLLocation *portland = [[CLLocation alloc] initWithLatitude:45.5236111 longitude:-122.675];
CLLocationDistance distance = [portland distanceFromLocation:sanFrancisco];

MKDistanceFormatter *formatter = [[MKDistanceFormatter alloc] init];
formatter.units = MKDistanceFormatterUnitsImperial;
NSLog(@&quot;%@&quot;, [formatter stringFromDistance:distance]); // 535 miles
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;hr&gt;

&lt;p&gt;就是这些了！这只是 iOS 7 超棒的新特性中很小的一部分。想要更多？看下 Apple Developer Center 中的文档 &lt;a href=&quot;https://developer.apple.com/library/ios/releasenotes/General/WhatsNewIniOS/Articles/iOS7.html&quot;&gt;&amp;quot;What&amp;#39;s New in iOS 7&amp;quot;&lt;/a&gt;。&lt;/p&gt;</content><author><name>Zuyang Kou</name></author><summary type="html">译自 NSHispter。</summary></entry><entry><title type="html">iOS app 中拨电话之后返回自己的 app</title><link href="https://leafduo.com/articles/2013/08/20/ios-app-after-the-call-returns-its-own-app/" rel="alternate" type="text/html" title="iOS app 中拨电话之后返回自己的 app" /><published>2013-08-20T00:40:00+08:00</published><updated>2013-08-20T00:40:00+08:00</updated><id>https://leafduo.com/articles/2013/08/20/ios-app-after-the-call-returns-its-own-app</id><content type="html" xml:base="https://leafduo.com/articles/2013/08/20/ios-app-after-the-call-returns-its-own-app/">&lt;p&gt;iOS app 里面打电话应该算是个比较常见的需求了，一般常用的方法是利用 &lt;code&gt;UIApplication&lt;/code&gt; 里的 &lt;code&gt;-openURL:&lt;/code&gt; 方法，但是这样打完电话之后就会停留在 Phone.app 里面了。就像这样：&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objc&quot; data-lang=&quot;objc&quot;&gt;&lt;span class=&quot;n&quot;&gt;NSURL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneCallURL&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSURL&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;URLWithString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSString&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;stringWithFormat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;@&quot;tel:112&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UIApplication&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sharedApplication&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;openURL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneCallURL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;其实这种行为也是能预料到的，&lt;code&gt;tel:&lt;/code&gt; 肯定是 Phone.app 注册的嘛。&lt;/p&gt;

&lt;p&gt;不过利用 &lt;code&gt;UIWebView&lt;/code&gt; 就能在拨打电话之后返回自己的应用。&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objc&quot; data-lang=&quot;objc&quot;&gt;&lt;span class=&quot;n&quot;&gt;_phoneCallWebView&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;UIWebView&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;alloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;NSURLRequest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSURLRequest&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;alloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initWithURL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NSURL&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;URLWithString&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;@&quot;tel:112&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_phoneCallWebView&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;loadRequest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;用这个方法不需要自己添加确认的 action sheet 或者 alert view 了，会自动弹出来一个 alert view。另外可以用 &lt;code&gt;UIWebView&lt;/code&gt; 的 delegate 方法 &lt;code&gt;-webView:didFailLoadWithError:&lt;/code&gt; 来检测不能打电话的设备，在不能打电话的设备上打开 &lt;code&gt;tel:&lt;/code&gt; URL 的时候这个方法会被调用。&lt;/p&gt;</content><author><name>Zuyang Kou</name></author><summary type="html">iOS app 里面打电话应该算是个比较常见的需求了，一般常用的方法是利用 UIApplication 里的 -openURL: 方法，但是这样打完电话之后就会停留在 Phone.app 里面了。就像这样： NSURL *phoneCallURL = [NSURL URLWithString:[NSString stringWithFormat:@&quot;tel:112&quot;]]; [[UIApplication sharedApplication] openURL:phoneCallURL]; 其实这种行为也是能预料到的，tel: 肯定是 Phone.app 注册的嘛。 不过利用 UIWebView 就能在拨打电话之后返回自己的应用。 _phoneCallWebView = [[UIWebView alloc] init]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@&quot;tel:112&quot;]]; [_phoneCallWebView loadRequest:request]; 用这个方法不需要自己添加确认的 action sheet 或者 alert view 了，会自动弹出来一个 alert view。另外可以用 UIWebView 的 delegate 方法 -webView:didFailLoadWithError: 来检测不能打电话的设备，在不能打电话的设备上打开 tel: URL 的时候这个方法会被调用。</summary></entry><entry><title type="html">GPUImage</title><link href="https://leafduo.com/articles/2013/05/13/gpuimage/" rel="alternate" type="text/html" title="GPUImage" /><published>2013-05-13T00:38:00+08:00</published><updated>2013-05-13T00:38:00+08:00</updated><id>https://leafduo.com/articles/2013/05/13/gpuimage</id><content type="html" xml:base="https://leafduo.com/articles/2013/05/13/gpuimage/">&lt;p&gt;（翻译自 NSHispter，原文链接：&lt;a href=&quot;http://nshipster.com/gpuimage/&quot;&gt;http://nshipster.com/gpuimage/&lt;/a&gt;）&lt;/p&gt;

&lt;p&gt;在 NSHipster，我们一直在研究 Objective-C 不为人知的一面来了解我们每天面对的系统。一般来说，这意味着仔细研究 Apple 的框架和语言特性（毕竟每天要用 Obective-C 工作）。不过，&lt;a href=&quot;http://nshipster.com/reactivecocoa/&quot;&gt;有些时候&lt;/a&gt;，看看成堆的第三方库和框架也蛮好的（而且里面有些真的很好），还可以看一下 Cupertino 外面有什么新东西。&lt;/p&gt;

&lt;p&gt;本周我们来看一下你能找到的最厉害的一个开源项目：&lt;a href=&quot;https://github.com/BradLarson/GPUImage&quot;&gt;GPUImage&lt;/a&gt;。系好安全带，NSHipster 们，一不小心你读完这篇文章就造出一个相机 app 了。&lt;/p&gt;

&lt;hr&gt;

&lt;p&gt;GPUImage 作者是 &lt;a href=&quot;https://github.com/BradLarson/&quot;&gt;Brad Larson&lt;/a&gt;，以 BSD 协议放出，能够在图像、实时摄像头影像和视频上使用 GPU 加速的滤镜和其他效果。&lt;/p&gt;

&lt;h2&gt;GPU vs. CPU&lt;/h2&gt;

&lt;p&gt;每只 iPhone 都有两个处理器：一个 &lt;a href=&quot;http://en.wikipedia.org/wiki/Central_processing_unit&quot;&gt;CPU&lt;/a&gt;，也就是中央处理器，和一个 &lt;a href=&quot;http://en.wikipedia.org/wiki/Graphics_processing_unit&quot;&gt;GPU&lt;/a&gt;，也就是图形处理器。每个处理器都有它自己的优势，现代芯片架构（例如 Apple &lt;a href=&quot;http://en.wikipedia.org/wiki/Apple_A4&quot;&gt;A4&lt;/a&gt;）把 CPU 和 GPU 集成在一个物理封装里。&lt;/p&gt;

&lt;p&gt;在 Xcode 里面写 C 和 Objective-C 的时候，产生的指令绝大部分会被 CPU 执行。相对地，GPU 是一枚专用芯片，专门用来做独立的小操作，例如图形渲染。GPU 执行的指令和 CPU 是有很大区别的，因此用特殊的语言来编写：&lt;a href=&quot;http://en.wikipedia.org/wiki/Opengl&quot;&gt;OpenGL&lt;/a&gt;（特别地，在 iPhone 和 iPad 上使用 &lt;a href=&quot;http://en.wikipedia.org/wiki/OpenGL_ES&quot;&gt;OpenGL ES&lt;/a&gt;） &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;如果想学习 OpenGL ES 和渲染管线相关的知识，可以参考 &lt;a href=&quot;http://iphonedevelopment.blogspot.com/2010/11/opengl-es-20-for-ios-chapter-4.html&quot;&gt;Jeff LaMarche&amp;#39;s GLProgram OpenGL ES 2.0 book&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;比较一下 GPU 渲染和 CPU 渲染的性能，区别还是蛮大的：&lt;/p&gt;

&lt;table id=&quot;gpuimage-benchmarks&quot;&gt;
  &lt;summary&gt;CPU vs. GPU 帧率对比 (FPS 越高越好)&lt;/summary&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Calculation&lt;/th&gt;
      &lt;th&gt;GPU FPS&lt;/th&gt;
      &lt;th&gt;CPU FPS&lt;/th&gt;
      &lt;th&gt;Δ&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Thresholding ⨉ 1&lt;/td&gt;
      &lt;td&gt;60.00&lt;/td&gt;
      &lt;td&gt;4.21&lt;/td&gt;
      &lt;td&gt;14.3⨉&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thresholding ⨉ 2&lt;/td&gt;
      &lt;td&gt;33.63&lt;/td&gt;
      &lt;td&gt;2.36&lt;/td&gt;
      &lt;td&gt;14.3⨉&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Thresholding ⨉ 100&lt;/td&gt;
      &lt;td&gt;1.45&lt;/td&gt;
      &lt;td&gt;0.05&lt;/td&gt;
      &lt;td&gt;28.7⨉&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;h3&gt;&amp;quot;哦，这货是 Instagram？&amp;quot;&lt;/h3&gt;

&lt;p&gt;好吧，我们这么说：&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-&quot; data-lang=&quot;&quot;&gt;Instagram : GPUImage :: 拍立得 : NASA 空间光学制造中心
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;换种说法，用 GPUImage 的 API 可以做出&lt;em&gt;数千种&lt;/em&gt;相机 app，只需要把滤镜正确地组合起来，外加一小点想象力。&lt;/p&gt;

&lt;p&gt;这张表包含了 GPUImage 内置的 125 种滤镜：（译者：你想翻译死我吗？）&lt;/p&gt;

&lt;table id=&quot;gpuimage-filters&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Color Adjustments&lt;/th&gt;
      &lt;th&gt;Image Processing&lt;/th&gt;
      &lt;th&gt;Blending Modes&lt;/th&gt;
      &lt;th&gt;Visual Effects&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;
        &lt;ul&gt;
          &lt;li&gt;Brightness Filter&lt;/li&gt;
          &lt;li&gt;Exposure Filter&lt;/li&gt;
          &lt;li&gt;Contrast Filter&lt;/li&gt;
          &lt;li&gt;Saturation Filter&lt;/li&gt;
          &lt;li&gt;Gamma Filter&lt;/li&gt;
          &lt;li&gt;Levels Filter&lt;/li&gt;
          &lt;li&gt;Color Matrix Filter&lt;/li&gt;
          &lt;li&gt;RGB Filter&lt;/li&gt;
          &lt;li&gt;Hue Filter&lt;/li&gt;
          &lt;li&gt;Tone Curve Filter&lt;/li&gt;
          &lt;li&gt;Highlight Shadow Filter&lt;/li&gt;
          &lt;li&gt;Lookup Filter&lt;/li&gt;
          &lt;li&gt;Amatorka Filter&lt;/li&gt;
          &lt;li&gt;Miss Etikate Filter&lt;/li&gt;
          &lt;li&gt;Soft Elegance Filter&lt;/li&gt;
          &lt;li&gt;Color Invert Filter&lt;/li&gt;
          &lt;li&gt;Grayscale Filter&lt;/li&gt;
          &lt;li&gt;Monochrome Filter&lt;/li&gt;
          &lt;li&gt;False Color Filter&lt;/li&gt;
          &lt;li&gt;Haze Filter&lt;/li&gt;
          &lt;li&gt;Sepia Filter&lt;/li&gt;
          &lt;li&gt;Opacity Filter&lt;/li&gt;
          &lt;li&gt;Solid Color Generator&lt;/li&gt;
          &lt;li&gt;Luminance Threshold Filter&lt;/li&gt;
          &lt;li&gt;Adaptive Threshold Filter&lt;/li&gt;
          &lt;li&gt;Average Luminance Threshold Filter&lt;/li&gt;
          &lt;li&gt;Histogram Filter&lt;/li&gt;
          &lt;li&gt;Histogram Generator&lt;/li&gt;
          &lt;li&gt;Average Color&lt;/li&gt;
          &lt;li&gt;Luminosity&lt;/li&gt;
          &lt;li&gt;Chroma Key Filter&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;ul&gt;
          &lt;li&gt;Transform Filter&lt;/li&gt;
          &lt;li&gt;Crop Filter&lt;/li&gt;
          &lt;li&gt;Lanczos Resampling Filter&lt;/li&gt;
          &lt;li&gt;Sharpen Filter&lt;/li&gt;
          &lt;li&gt;Unsharp Mask Filter&lt;/li&gt;
          &lt;li&gt;Fast Blur Filter&lt;/li&gt;
          &lt;li&gt;Single Component Fast Blur Filter&lt;/li&gt;
          &lt;li&gt;Gaussian Blur Filter&lt;/li&gt;
          &lt;li&gt;Single Component Gaussian Blur Filter&lt;/li&gt;
          &lt;li&gt;Gaussian Selective Blur Filter&lt;/li&gt;
          &lt;li&gt;Gaussian Blur Position Filter&lt;/li&gt;
          &lt;li&gt;Median Filter&lt;/li&gt;
          &lt;li&gt;Bilateral Filter&lt;/li&gt;
          &lt;li&gt;Tilt Shift Filter&lt;/li&gt;
          &lt;li&gt;Box Blur Filter&lt;/li&gt;
          &lt;li&gt;3x3 Convolution Filter&lt;/li&gt;
          &lt;li&gt;Sobel Edge Detection Filter&lt;/li&gt;
          &lt;li&gt;Threshold Edge Detection Filter&lt;/li&gt;
          &lt;li&gt;Canny Edge Detection Filter&lt;/li&gt;
          &lt;li&gt;Harris Corner Detection Filter&lt;/li&gt;
          &lt;li&gt;Noble Corner Detection Filter&lt;/li&gt;
          &lt;li&gt;Shi-Tomasi Corner Detection Filter&lt;/li&gt;
          &lt;li&gt;Non Maximum Suppression Filter&lt;/li&gt;
          &lt;li&gt;X/Y Derivative Filter&lt;/li&gt;
          &lt;li&gt;Crosshair Generator&lt;/li&gt;
          &lt;li&gt;Dilation Filter&lt;/li&gt;
          &lt;li&gt;RGB Dilation Filter&lt;/li&gt;
          &lt;li&gt;Erosion Filter&lt;/li&gt;
          &lt;li&gt;RGB Erosion Filter&lt;/li&gt;
          &lt;li&gt;Opening Filter&lt;/li&gt;
          &lt;li&gt;RGB Opening Filter&lt;/li&gt;
          &lt;li&gt;Closing Filter&lt;/li&gt;
          &lt;li&gt;RGB Closing Filter&lt;/li&gt;
          &lt;li&gt;Local Binary Pattern Filter&lt;/li&gt;
          &lt;li&gt;Low Pass Filter&lt;/li&gt;
          &lt;li&gt;High Pass Filter&lt;/li&gt;
          &lt;li&gt;Motion Detector&lt;/li&gt;
          &lt;li&gt;Hough Transform Line Detector&lt;/li&gt;
          &lt;li&gt;Line Generator&lt;/li&gt;
          &lt;li&gt;Motion Blur Filter&lt;/li&gt;
          &lt;li&gt;Zoom Blur Filter&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;ul&gt;
          &lt;li&gt;Chroma Key Blend Filter&lt;/li&gt;
          &lt;li&gt;Dissolve Blend Filter&lt;/li&gt;
          &lt;li&gt;Multiply Blend Filter&lt;/li&gt;
          &lt;li&gt;Add Blend Filter&lt;/li&gt;
          &lt;li&gt;Subtract Blend Filter&lt;/li&gt;
          &lt;li&gt;Divide Blend Filter&lt;/li&gt;
          &lt;li&gt;Overlay Blend Filter&lt;/li&gt;
          &lt;li&gt;Darken Blend Filter&lt;/li&gt;
          &lt;li&gt;Lighten Blend Filter&lt;/li&gt;
          &lt;li&gt;Color Burn Blend Filter&lt;/li&gt;
          &lt;li&gt;Color Dodge Blend Filter&lt;/li&gt;
          &lt;li&gt;Screen Blend Filter&lt;/li&gt;
          &lt;li&gt;Exclusion Blend Filter&lt;/li&gt;
          &lt;li&gt;Difference Blend Filter&lt;/li&gt;
          &lt;li&gt;Hard Light Blend Filter&lt;/li&gt;
          &lt;li&gt;Soft Light Blend Filter&lt;/li&gt;
          &lt;li&gt;Alpha Blend Filter&lt;/li&gt;
          &lt;li&gt;Source Over Blend Filter&lt;/li&gt;
          &lt;li&gt;Color Burn Blend Filter&lt;/li&gt;
          &lt;li&gt;Color Dodge Blend Filter&lt;/li&gt;
          &lt;li&gt;Normal Blend Filter&lt;/li&gt;
          &lt;li&gt;Color Blend Filter&lt;/li&gt;
          &lt;li&gt;Hue Blend Filter&lt;/li&gt;
          &lt;li&gt;Saturation Blend Filter&lt;/li&gt;
          &lt;li&gt;Luminosity Blend Filter&lt;/li&gt;
          &lt;li&gt;Linear Burn Blend Filter&lt;/li&gt;
          &lt;li&gt;Poisson Blend Filter&lt;/li&gt;
          &lt;li&gt;Mask Filter&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;ul&gt;
          &lt;li&gt;Pixellate Filter&lt;/li&gt;
          &lt;li&gt;Polar Pixellate Filter&lt;/li&gt;
          &lt;li&gt;Polka Dot Filter&lt;/li&gt;
          &lt;li&gt;Halftone Filter&lt;/li&gt;
          &lt;li&gt;Crosshatch Filter&lt;/li&gt;
          &lt;li&gt;Sketch Filter&lt;/li&gt;
          &lt;li&gt;Threshold Sketch Filter&lt;/li&gt;
          &lt;li&gt;Toon Filter&lt;/li&gt;
          &lt;li&gt;Smooth Toon Filter&lt;/li&gt;
          &lt;li&gt;Emboss Filter&lt;/li&gt;
          &lt;li&gt;Posterize Filter&lt;/li&gt;
          &lt;li&gt;Swirl Filter&lt;/li&gt;
          &lt;li&gt;Bulge Distortion Filter&lt;/li&gt;
          &lt;li&gt;Pinch Distortion Filter&lt;/li&gt;
          &lt;li&gt;Stretch Distortion Filter&lt;/li&gt;
          &lt;li&gt;Sphere Refraction Filter&lt;/li&gt;
          &lt;li&gt;Glass Sphere Filter&lt;/li&gt;
          &lt;li&gt;Vignette Filter&lt;/li&gt;
          &lt;li&gt;Kuwahara Filter&lt;/li&gt;
          &lt;li&gt;Kuwahara Radius 3 Filter&lt;/li&gt;
          &lt;li&gt;Perlin Noise Filter&lt;/li&gt;
          &lt;li&gt;CGAColorspace Filter&lt;/li&gt;
          &lt;li&gt;Mosaic Filter&lt;/li&gt;
          &lt;li&gt;JFAVoronoi Filter&lt;/li&gt;
          &lt;li&gt;Voronoi Consumer Filter&lt;/li&gt;
        &lt;/ul&gt;
      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;认真说，GPUImage 代码里带的这个 &lt;a href=&quot;https://github.com/BradLarson/GPUImage/tree/master/examples/iOS/FilterShowcase&quot;&gt;滤镜展示示例 app&lt;/a&gt; 能在 AppStore 卖 3.99 刀。要是再加上 twitter 集成和一些声音效果，卖个 6.99 刀不成问题。&lt;/p&gt;

&lt;h2&gt;渲染管线&lt;/h2&gt;

&lt;p&gt;&lt;object data=&quot;http://nshipster.s3.amazonaws.com/gpuimage-pipeline.svg&quot; type=&quot;image/svg+xml&quot;&gt;
  &lt;img src=&quot;http://nshipster.s3.amazonaws.com/gpuimage-pipeline.png&quot; /&gt;
&lt;/object&gt;&lt;/p&gt;

&lt;p&gt;GPUImage 从本质上来说是一个渲染管线的 Objective-C 抽象。从相机、网络或者磁盘上加载的源图像，在经过一系列滤镜处理后，最终输出到了 view、graphics context 或是数据流中。&lt;/p&gt;

&lt;p&gt;例如说，摄像头中的图像可以应用一个 Color Levels 滤镜，来模拟各种色盲效果，然后实时显示在一个 view 中。&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objc&quot; data-lang=&quot;objc&quot;&gt;&lt;span class=&quot;n&quot;&gt;GPUImageVideoCamera&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;videoCamera&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPUImageVideoCamera&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;alloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;initWithSessionPreset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AVCaptureSessionPreset640x480&lt;/span&gt;
           &lt;span class=&quot;nf&quot;&gt;cameraPosition&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;AVCaptureDevicePositionBack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;videoCamera&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;outputImageOrientation&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;UIInterfaceOrientationPortrait&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;GPUImageFilter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPUImageLevelsFilter&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;alloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initWithFragmentShaderFromFile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;@&quot;CustomShader&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setRedMin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;299&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gamma&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;minOut&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxOut&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setGreenMin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;587&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gamma&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;minOut&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxOut&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setBlueMin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;114&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;gamma&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;minOut&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maxOut&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;videoCamera&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;addTarget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;GPUImageView&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filteredVideoView&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPUImageView&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;alloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initWithFrame&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;view&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bounds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;addTarget&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filteredVideoView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;view&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;addSubView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filteredVideoView&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;videoCamera&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;startCameraCapture&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;或者，结合不同的颜色混合模式，图像效果和一些调整，你可以把静止图像转变成可以分享给你时髦的朋友们看看的精美图像（例子来自基于 GPUImage 的 &lt;a href=&quot;https://github.com/eklipse2k8/FilterKit/blob/master/FilterKit/FilterKit/Filters/FKBlueValentine.m&quot;&gt;FilterKit&lt;/a&gt;）。&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objc&quot; data-lang=&quot;objc&quot;&gt;&lt;span class=&quot;n&quot;&gt;GPUImageFilterGroup&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;FKGPUFilterGroup&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;alloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;GPUImageSaturationFilter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;saturationFilter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPUImageSaturationFilter&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;alloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;saturationFilter&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setSaturation&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;GPUImageMonochromeFilter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;monochromeFilter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPUImageMonochromeFilter&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;alloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;monochromeFilter&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setColor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPUVector4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;){&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;monochromeFilter&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setIntensity&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;GPUImageVignetteFilter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vignetteFilter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPUImageVignetteFilter&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;alloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vignetteFilter&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setVignetteEnd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;GPUImageExposureFilter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exposureFilter&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GPUImageExposureFilter&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;alloc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;init&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exposureFilter&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setExposure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;addGPUFilter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exposureFilter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;addGPUFilter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;monochromeFilter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;addGPUFilter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;saturationFilter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;filter&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;addGPUFilter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vignetteFilter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;];&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;hr&gt;

&lt;p&gt;看完了 GPUImage 的功能，一定很兴奋吧。GPUImage 非常简单，可以马上开始使用（不需要了解 OpenGL），性能上也足够完成你想要的一切。不止是这样，它还自带了非常多的积木一样的部件 -- 色彩调整，混合模式，你能想到的想不到的一切视觉效果。&lt;/p&gt;

&lt;p&gt;GPUImage 是开源社区少有的好东西，我们 Mac 和 iOS 开发者非常幸运的能拥有它。用 GPUImage 做些牛逼的东西，向其他人展示一个崭新的世界。&lt;/p&gt;

&lt;hr&gt;

&lt;h2&gt;译者吐槽时间&lt;/h2&gt;

&lt;p&gt;我自己也用过一点 GPUImage，方便是很方便，不光是省掉了 AVFoundation 的许多烦人的 setup 代码，还内置了许多滤镜效果。不过它也有不少坑，我遇到的一个就是占用太多内存，一段时间后就 crash 掉了，毕竟是处理视频，需要不少内存，iPhone 的内存又不太多，就要更加注意。我当时是加了一个 @autoreleasepool 就好多了。&lt;/p&gt;</content><author><name>Zuyang Kou</name></author><summary type="html">（翻译自 NSHispter，原文链接：http://nshipster.com/gpuimage/）</summary></entry><entry><title type="html">A NSFetchedResultsController Pitfall</title><link href="https://leafduo.com/articles/2013/04/15/nsfetchedresultscontroller-pitfall/" rel="alternate" type="text/html" title="A NSFetchedResultsController Pitfall" /><published>2013-04-15T09:50:00+08:00</published><updated>2013-04-15T09:50:00+08:00</updated><id>https://leafduo.com/articles/2013/04/15/nsfetchedresultscontroller-pitfall</id><content type="html" xml:base="https://leafduo.com/articles/2013/04/15/nsfetchedresultscontroller-pitfall/">&lt;p&gt;&lt;img src=&quot;/assets/images/%E3%81%86%E3%81%97%E3%81%8D%20-%20%E6%AD%8C%E3%81%86%E6%A8%99%E8%AD%98%20(2183700)%20.jpg&quot; alt=&quot;image&quot; title=&quot;pixiv id=2183700&quot;&gt;&lt;/p&gt;

&lt;p&gt;I&amp;#39;m using Core Data and &lt;code&gt;NSFetchedResultsController&lt;/code&gt;, and encountered mystery crashes related to them. Here&amp;#39;s the solution.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&quot;http://developer.apple.com/library/ios/#documentation/CoreData/Reference/NSFetchedResultsController_Class/Reference/Reference.html#jumpTo_2&quot;&gt;document from Apple&lt;/a&gt; mentions that if delegate is not &lt;code&gt;nil&lt;/code&gt;, then &lt;code&gt;NSFetchedResultsController&lt;/code&gt; will track changes to managed objects, by listening to notifications sent by managed object context and then call its delegate to modify corresponding &lt;code&gt;UITableView&lt;/code&gt;. This &lt;em&gt;implies&lt;/em&gt; if you don&amp;#39;t set delegate to &lt;code&gt;nil&lt;/code&gt; before it gets dealloced, then observation is still registered with &lt;code&gt;NSFetchedResultsController&lt;/code&gt; and cause strange messages and crashes. We may see the following messages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;-[SomeStrangeReciver controllerWillChangeContent:]: unrecognized selector sent to instance 0x8251dc0&lt;/code&gt;, the receiver is random, I&amp;#39;ve seen &lt;code&gt;__NSCFType&lt;/code&gt;, &lt;code&gt;UITapGestureRecognizer&lt;/code&gt;, etc.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Assertion failure in -[UITableView _endCellAnimationsWithContext:] …… Terminating app due to uncaught exception &amp;#39;NSInternalInconsistencyException&amp;#39;, reason: &amp;#39;Invalid update: invalid number of rows in section 0. The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (0), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted).&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;EXC&lt;em&gt;BAD&lt;/em&gt;ACCESS&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;(These error messages are from Google, and may be slightly different from actual messages)&lt;/p&gt;

&lt;p&gt;Though we see different messages, the reason is the same: The observation has not been cancelled after &lt;code&gt;NSFetchedResultsController&lt;/code&gt; gets dealloced.&lt;/p&gt;

&lt;p&gt;The solution is simple:&lt;/p&gt;
&lt;div class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-objc&quot; data-lang=&quot;objc&quot;&gt;&lt;span class=&quot;k&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dealloc&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;_myFetchedResultsController&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;_myFetchedResultsController&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;delegate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In &lt;a href=&quot;https://github.com/soffes/ssdatakit&quot;&gt;SSDataKit&lt;/a&gt;, the author did the same thing in &lt;code&gt;-dealloc&lt;/code&gt; (See &lt;a href=&quot;https://github.com/soffes/ssdatakit/blob/f73a343ffe0126eccf4ab6b452891a57b06818a9/SSDataKit/SSManagedViewController.m#L50&quot;&gt;SSManagedViewController.m&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;So,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read document carefully, it may have some implication;&lt;/li&gt;
&lt;li&gt;Think how it will be implemented;&lt;/li&gt;
&lt;li&gt;Memory management in Objective-C is still a little painful;&lt;/li&gt;
&lt;li&gt;See how others do it.&lt;/li&gt;
&lt;/ol&gt;</content><author><name>Zuyang Kou</name></author><summary type="html"></summary></entry><entry><title type="html">Lion 对加密卷的支持</title><link href="https://leafduo.com/articles/2011/07/18/lion-encrypted-volume-support/" rel="alternate" type="text/html" title="Lion 对加密卷的支持" /><published>2011-07-18T00:00:00+08:00</published><updated>2011-07-18T00:00:00+08:00</updated><id>https://leafduo.com/articles/2011/07/18/lion-encrypted-volume-support</id><content type="html" xml:base="https://leafduo.com/articles/2011/07/18/lion-encrypted-volume-support/">&lt;p&gt;今天想给硬盘数据做备份，毕竟硬盘已经用了两年了，不用查 SMART 信息也知道他已经老了，虽然诸如代码之类的都有很多备份，但是还是有些文档、照片需要备份。&lt;/p&gt;

&lt;p&gt;又阴差阳错得到一块老旧移动硬盘，灰常好啊。开工吧。&lt;/p&gt;

&lt;p&gt;拿来的时候是 NTFS，第一件事情就是改成 HFS+，拿 Disk Utility 格式化的时候发现多出了加密的选项，不用多说，先试试。不得不说这个 Lion 的 GM 版本还不够到正式版的水平，这个输入密码的输入框如果输入超过十位的密码就会 crash，这个完全不能忍受啊，尝试了半天发现先在别的地方输入好然后复制过来就可以了。（好吧，我不得不说我写文章想截图的时候这个 bug 又神秘的消失了）&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://picasaweb.google.com/lh/photo/jeCgDHO8qJqbYuM4Guqctg?feat=embedwebsite&quot;&gt;&lt;img src=&quot;https://lh4.googleusercontent.com/-ryRM1jL_il0/TiMs7wbtP1I/AAAAAAAACcY/SSqM8_GCDko/s640/diskutil.png&quot; height=&quot;528&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;这样格式化好了之后每次插入移动硬盘的时候都会提示你输入密码才能访问，像是做备份这种应用就会安全很多。&lt;/p&gt;

&lt;p&gt;之后我就发现加密卷的密码无法修改，我猜测这种加密是用密码加密一个密钥，再用密钥加密数据的，应该可以修改密码，现在无法修改的原因应该是 Apple 还没想好在哪里放这个功能，也或许 Apple 不会给这个功能提供 GUI，那我们转战 CLI 吧，Disk Utility 对应的命令行工具是 diskutil，里面提供了很多 GUI 中没有的功能。鼓捣一会之后发现 diskutil 有个叫做 CoreStorage 的模块，进一步发现 &lt;code&gt;diskutil coreStorage changeVolumePassphrase&lt;/code&gt; 可以修改 CoreStorage 卷的密码，其中所需的卷 UUID 可以通过 &lt;code&gt;diskutil coreStorage list&lt;/code&gt; 获得。&lt;/p&gt;

&lt;p&gt;可以看得出，CoreStorage 又是苹果推的一种新技术，看 diskutil 里面的用语（PV、LV）推测是类似 LVM 的东西，Mac 以前似乎没有类似 LVM 的技术（AFAIK），网上搜索也搜索不到 CoreStorage 的相关信息，Lion 的 Developer Library 里面也是没有，最后还是在手册（man diskutil）里找到了一些信息。看完之后的感觉就是 CoreStorage 就是一个 LVM，Apple 终于有了 LVM。 :-) 不过看起来许多功能还没有开发出来接口，比如 LVM 的一些最基本的功能⋯⋯ :-(&lt;/p&gt;

&lt;p&gt;看来 Lion 不光是在界面上模仿 iOS（我对这点颇有不满），在底层也下了不少功夫，CoreStorage 就是在看不见的地方默默发挥作用的一个好同志啊。底层的 UNIX 配合 用户友好的界面，是 Mac 的制胜法宝，只有这样才能留住如此多的开发者和一些相当挑剔的用户（比如设计师）。底层的 BSD 也给苹果减少了许多开发的困难，可以到处抄来抄去，可以派几个人推动一下某个项目的开发（比如 LLVM），看被抄的人不爽了还过去帮助修改（比如各种 BSD）。相比之下，再大的微软也不能把所有事情做好，至少给自己增加了许多开发难度。&lt;/p&gt;

&lt;p&gt;另外，苹果你抄个 ZFS 过来吧，ZFS 多适合你现在的各种用途啊，又没有 Linux 的许可证问题，抓紧啊，I want a better filesystem!&lt;/p&gt;

&lt;p&gt;PS：最后贴上 diskutil 手册的一部分，如果你有兴趣的话。&lt;/p&gt;

&lt;p&gt;&lt;script src=&quot;https://gist.github.com/1087869.js?file=gistfile1.txt&quot;&gt;&lt;/script&gt;&lt;/p&gt;</content><author><name>Zuyang Kou</name></author><summary type="html">今天想给硬盘数据做备份，毕竟硬盘已经用了两年了，不用查 SMART 信息也知道他已经老了，虽然诸如代码之类的都有很多备份，但是还是有些文档、照片需要备份。</summary></entry><entry><title type="html">C Traps &amp;amp; Tips II – Digraphs and Trigraphs</title><link href="https://leafduo.com/articles/2010/07/27/c-traps-tips-ii-digraphs-and-trigraphs/" rel="alternate" type="text/html" title="C Traps &amp; Tips II -- Digraphs and Trigraphs" /><published>2010-07-27T00:00:00+08:00</published><updated>2010-07-27T00:00:00+08:00</updated><id>https://leafduo.com/articles/2010/07/27/c-traps-tips-ii-digraphs-and-trigraphs</id><content type="html" xml:base="https://leafduo.com/articles/2010/07/27/c-traps-tips-ii-digraphs-and-trigraphs/">&lt;p&gt;今天写个第二遍吧，这篇有点×疼，大家见谅。&lt;/p&gt;

&lt;p&gt;先看代码：&lt;/p&gt;

&lt;p&gt;&lt;pre lang=&quot;c&quot; line=&quot;1&quot;&gt;#include &lt;stdio.h /&gt;&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;int main()&lt;br /&gt;
{
    printf(&quot;%s&quot;, &quot;What's this??!\n&quot;);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;/p&gt;

&lt;p&gt;&lt;pre lang=&quot;c&quot; line=&quot;1&quot;&gt;int main()
{
  int a = 0;
  //What's a's value??????????????/
  a++;
  return 0;
}
&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;想想上面两段代码吧……&lt;/p&gt;

&lt;p&gt;好了，答案在这里，在符合 C99 的编译器里，第一段输出 What's this|，第二段 a 的值还是零。为什么这样呢，因为 C99 增加了一个叫 trigraph 的特性，也就是三元组。由于某些键盘上无法输入某些字符，所以就定义三个字符来替代那一个字符，比如 ??! 会转换成 |，??/ 会转换成 \，也就是说 a++ 也成了注释的一部分，（三元组都是两个问号开头的），而且无论是在字符串中还是注释中都会被替换（在预处理器中进行替换，不论在代码的哪里出现都会被替换）。这样就能解释上面两段代码的输出了。想要避免这个问题可以将 ? 进行转义（写成 \?），或者把字符串拆开（写成 &quot;?&quot;&quot;?&quot;&quot;!&quot;）。&lt;/p&gt;

&lt;p&gt;还有一种叫做 digraph，二元组，也就是用两个字符来表示一个字符。与三元组不同的是，二元组被当成一个独立的 token 来解释，在预处理器进行词法分析的时候被替换，也就是说，在字符串中和包括在其他 token 中的二元组不会被替换掉，这样就不会造成像三元组那样诡异的问题了。&lt;/p&gt;

&lt;p&gt;为啥说这篇×疼的，是因为 GCC 默认不开启三元组的支持（M$ 的编译器不清楚）……想要开启需要在编译时指定 -std 或者 -trigraphs，没有这两个编译选项的时候出现三元组不会发生替换，编译时会有警告。&lt;/p&gt;

&lt;p&gt;我觉得这东西平时没啥用，不要误用了就好。&lt;/p&gt;

&lt;p&gt;References：&lt;/p&gt;

&lt;p&gt;&lt;ul&gt;
	&lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Digraphs_and_trigraphs&quot;&gt;http://en.wikipedia.org/wiki/Digraphs_and_trigraphs&lt;/a&gt;
&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://gcc.gnu.org/onlinedocs/cpp/Initial-processing.html&quot;&gt;http://gcc.gnu.org/onlinedocs/cpp/Initial-processing.html&lt;/a&gt;
&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://gcc.gnu.org/onlinedocs/cpp/Tokenization.html&quot;&gt;http://gcc.gnu.org/onlinedocs/cpp/Tokenization.html&lt;/a&gt;
&lt;/li&gt;
	&lt;li&gt;&lt;a href=&quot;http://stackoverflow.com/questions/1995113/strangest-language-feature/1995134#1995134&quot;&gt;http://stackoverflow.com/questions/1995113/strangest-language-feature/1995134#1995134&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;
&lt;/p&gt;</content><author><name>Zuyang Kou</name></author><category term="C++" /><summary type="html">今天写个第二遍吧，这篇有点×疼，大家见谅。</summary></entry><entry><title type="html">C Traps &amp;amp; Tips I – Duff’s Device</title><link href="https://leafduo.com/articles/2010/07/22/c-traps-tips-i-duffs-device/" rel="alternate" type="text/html" title="C Traps &amp; Tips I -- Duff's Device" /><published>2010-07-22T00:00:00+08:00</published><updated>2010-07-22T00:00:00+08:00</updated><id>https://leafduo.com/articles/2010/07/22/c-traps-tips-i-duffs-device</id><content type="html" xml:base="https://leafduo.com/articles/2010/07/22/c-traps-tips-i-duffs-device/">&lt;p&gt;最近又对 C 的陷阱和奇怪的用法之类的感兴趣了，于是又想写点东西，完全是主观感觉奇怪的或者是感觉是陷阱就写出来了，外加无责任分析，错了还请大家斧正……&lt;/p&gt;

&lt;p&gt;今天写第一篇，希望以后能多写点……&lt;/p&gt;

&lt;p&gt;先上代码：&lt;/p&gt;

&lt;p&gt;&lt;pre lang=&quot;c&quot; line=&quot;1&quot;&gt;send(to, from, count)
register short *to, *from;
register count;
{
	register n=(count+7)/8;
	switch(count%8){
	case 0:	do{	*to = *from++;
	case 7:		*to = *from++;
	case 6:		*to = *from++;
	case 5:		*to = *from++;
	case 4:		*to = *from++;
	case 3:		*to = *from++;
	case 2:		*to = *from++;
	case 1:		*to = *from++;
		}while(--n&gt;0);
	}
}
&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;看懂了的话下面可以无视了……请轻轻按下左上角或者右上角的X……&lt;/p&gt;

&lt;p&gt;可以看出，这短代码是用来复制内存的（请别告诉我你看不懂旧式的函数首部），就像 memcpy，一般人可能会写出类似下面的代码：&lt;/p&gt;

&lt;p&gt;&lt;pre lang=&quot;c&quot; line=&quot;1&quot;&gt;do {                          /* count &gt; 0 assumed */
    *to = *from++;            /* Note that the ''to'' pointer is NOT incremented */
} while (--count &gt; 0);
&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;这两段代码的区别是上面的一个，也就是 Duff's device 用了叫做循环展开的技术，观察可以发现，下面这段代码循环体很短，也就是说循环条件的判断在循环执行的时候占用了很大一部分的时间，而循环展开后循环条件所占用的时间下降了，从而提高了程序的效率。&lt;/p&gt;

&lt;p&gt;其实 Duff's device 里面最神奇的就是 switch 跟 while 交错出现。示例中循环展开了 8 次，第一次 switch 的时候首先处理被 8 除余下的 count%8 次，然后 while 循环处理 8*(n-1) 次，这样就正好凑齐 count 次复制了。这个 8 次是可以改的，愿意的话可以改成 16 次？不过展开越多二进制的大小就越大，也算是用空间换时间了。&lt;/p&gt;

&lt;p&gt;至于你问我为啥是 &lt;span style=&quot;font-family: monospace;&quot;&gt;*to&lt;/span&gt; 而不是 &lt;span style=&quot;font-family: monospace;&quot;&gt;*to++&lt;/span&gt;，我只能告诉你那个指针 &lt;span style=&quot;font-family: monospace;&quot;&gt;to&lt;/span&gt; 不是一般的指针，而是 memory-mapped output register，按我的理解大概是它能自动增长吧？参考资料里说新手可能不知道 memory-mapped output register，所以有时候就改成 &lt;span style=&quot;font-family: monospace;&quot;&gt;*to++&lt;/span&gt; 了，嗯，我是新手。谁知道是怎么回事告诉我一声？查了一些还是不太懂……&lt;/p&gt;

&lt;p&gt;另外，Duff's device 也不是说一定能让代码变快，有人就说了，我去掉这些烦人的 Duff's device 之后反而快了。 :-(&lt;/p&gt;

&lt;p&gt;References:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Duff's_device&quot;&gt;http://en.wikipedia.org/wiki/Duff's_device&lt;/a&gt;&lt;/p&gt;</content><author><name>Zuyang Kou</name></author><category term="C++" /><summary type="html">最近又对 C 的陷阱和奇怪的用法之类的感兴趣了，于是又想写点东西，完全是主观感觉奇怪的或者是感觉是陷阱就写出来了，外加无责任分析，错了还请大家斧正……</summary></entry></feed>