經過前幾篇博客的介紹,咱們瞭解到擴展給app提供的更增強大的交互能力,這種強大的交互能力另外一方面體如今照片編輯插件的應用。app
和一般同樣,咱們先建立一個工程,而後新建一個Target,選擇photo editing:async
從模板中,咱們能夠看到系統爲咱們建立了一個controller,這個controller就是用於處理照片的controller,其中方法以下:ide
- (BOOL)canHandleAdjustmentData:(PHAdjustmentData *)adjustmentData { // Inspect the adjustmentData to determine whether your extension can work with past edits. // (Typically, you use its formatIdentifier and formatVersion properties to do this.) return NO; } //這個函數用於從系統相冊獲取到選中的照片,contentEditingInput對象中存有響應的數據類型和image對象 - (void)startContentEditingWithInput:(PHContentEditingInput *)contentEditingInput placeholderImage:(UIImage *)placeholderImage { //咱們能夠在這裏將取到的數據進行展現等等 self.input = contentEditingInput; } //結束編輯照片時的方法 - (void)finishContentEditingWithCompletionHandler:(void (^)(PHContentEditingOutput *))completionHandler { // Update UI to reflect that editing has finished and output is being rendered. // Render and provide output on a background queue. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Create editing output from the editing input. PHContentEditingOutput *output = [[PHContentEditingOutput alloc] initWithContentEditingInput:self.input]; //咱們能夠在這裏將新的圖片數據寫入到輸出流中 // output.adjustmentData = <#new adjustment data#>; // NSData *renderedJPEGData = <#output JPEG#>; // [renderedJPEGData writeToURL:output.renderedContentURL atomically:YES]; // Call completion handler to commit edit to Photos. completionHandler(output); // Clean up temporary files, etc. }); }
在當前擴展執行結束編輯以前,咱們能夠自由渲染咱們獲得的圖片,例如添加相框,文字等等,輸出時將渲染後的圖片進行輸出便可。函數
這裏還有一個地方須要咱們注意,此類擴展有一個功能,若是咱們中途退出編輯,系統會爲咱們保存咱們擴展的處理狀態,爲了區分多個相似功能的擴展,在輸出數據的對象中有一個PHAdjustmentData類型的對象,這個對象專門用於負責版本的記錄,這個對象中有以下兩個屬性用於區分版本:this
@property (readonly, copy) NSString *formatIdentifier;atom
@property (readonly, copy) NSString *formatVersion;spa
專一技術,熱愛生活,交流技術,也作朋友。.net
——琿少 QQ羣:203317592插件