天天看點

iOS8新特性擴充(Extension)應用之三——照片編輯插件

        通過前幾篇部落格的介紹,我們了解到擴充給app提供的更加強大的互動能力,這種強大的互動能力另一方面展現在照片編輯插件的應用。

       和通常一樣,我們先建立一個工程,然後建立一個target,選擇photo editing:

iOS8新特性擴充(Extension)應用之三——照片編輯插件

從模闆中,我們可以看到系統為我們建立了一個controller,這個controller就是用于處理照片的controller,其中方法如下:

<a href="http://my.oschina.net/u/2340880/blog/485827#">?</a>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

<code>- (</code><code>bool</code><code>)canhandleadjustmentdata:(phadjustmentdata *)adjustmentdata {</code>

<code>    </code><code>// inspect the adjustmentdata to determine whether your extension can work with past edits.</code>

<code>    </code><code>// (typically, you use its formatidentifier and formatversion properties to do this.)</code>

<code>    </code><code>return</code> <code>no;</code>

<code>}</code>

<code>//這個函數用于從系統相冊擷取到選中的照片,contenteditinginput對象中存有響應的資料類型和image對象</code>

<code>- (</code><code>void</code><code>)startcontenteditingwithinput:(phcontenteditinginput *)contenteditinginput placeholderimage:(uiimage *)placeholderimage {</code>

<code>   </code><code>//我們可以在這裡将取到的資料進行展示等等</code>

<code>    </code><code>self.input = contenteditinginput;</code>

<code>//結束編輯照片時的方法</code>

<code>- (</code><code>void</code><code>)finishcontenteditingwithcompletionhandler:(</code><code>void</code> <code>(^)(phcontenteditingoutput *))completionhandler {</code>

<code>    </code><code>// update ui to reflect that editing has finished and output is being rendered.</code>

<code>    </code> 

<code>    </code><code>// render and provide output on a background queue.</code>

<code>    </code><code>dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{</code>

<code>        </code><code>// create editing output from the editing input.</code>

<code>        </code><code>phcontenteditingoutput *output = [[phcontenteditingoutput alloc] initwithcontenteditinginput:self.input];</code>

<code>        </code> 

<code>        </code><code>//我們可以在這裡将新的圖檔資料寫入到輸出流中</code>

<code>        </code><code>// output.adjustmentdata = &lt;#new adjustment data#&gt;;</code>

<code>        </code><code>// nsdata *renderedjpegdata = &lt;#output jpeg#&gt;;</code>

<code>        </code><code>// [renderedjpegdata writetourl:output.renderedcontenturl atomically:yes];</code>

<code>        </code><code>// call completion handler to commit edit to photos.</code>

<code>        </code><code>completionhandler(output);</code>

<code>        </code><code>// clean up temporary files, etc.</code>

<code>    </code><code>});</code>

在目前擴充執行結束編輯之前,我們可以自由渲染我們得到的圖檔,例如添加相框,文字等等,輸出時将渲染後的圖檔進行輸出即可。

這裡還有一個地方需要我們注意,此類擴充有一個功能,如果我們中途退出編輯,系統會為我們儲存我們擴充的處理狀态,為了區分多個類似功能的擴充,在輸出資料的對象中有一個phadjustmentdata類型的對象,這個對象專門用于負責版本的記錄,這個對象中有如下兩個屬性用于區分版本:

@property (readonly, copy) nsstring *formatidentifier;

@property (readonly, copy) nsstring *formatversion;