2016-01-29 32 views
6

我的照片擴展應用程序可以訪問相機和照片。 一切正常,但按完成,它不能保存圖像。iOS照片擴展程序finishContentEditingWithCompletionHandler:無法保存更改

代碼標準完成處理程序的:

- (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), ^{ 
     PHContentEditingOutput *output = [[PHContentEditingOutput alloc] initWithContentEditingInput:self.input]; 

     NSError* error = nil; 

     NSData *renderedJPEGData = UIImageJPEGRepresentation(filtered_ui_image, 1.0); 
     assert(renderedJPEGData != nil); 
     //BOOL written_well = [renderedJPEGData writeToURL:output.renderedContentURL atomically:YES]; 

     BOOL written_well = [renderedJPEGData writeToURL:output.renderedContentURL options:NSDataWritingAtomic error:&error]; 
     assert(written_well); 



     // Call completion handler to commit edit to Photos. 
     completionHandler(output); 
    }); 
} 

renderedJPEGData不是nil
errornil,從而功能[NSData writeToURL]是成功的,
written_wellYES

調試時線逐行,塊完成後,會出現一個警報: enter image description here

output.renderedContentURL/private/var/mobile/Containers/Data/PluginKitPlugin/509C1A04-D414-4DB7-B1E6-83C47FC88BC9/tmp/blah_blah_name.JPG

所以,我的權限,調試未顯示任何錯誤,我能嘗試檢測問題的原因是什麼?

回答

0

即使頭意味着adjustmentData可以是零,該文件規定:

如果你寫新的資產內容由renderedContentURL屬性指定的URL,你還必須提供一個新的,不同的PHAdjustmentData描述您編輯的對象。傳遞先前存在的調整數據對象(描述了較早的編輯)會導致未定義的行爲。

所以做這樣的事情在調用完成處理程序之前:

output.adjustmentData = [[PHAdjustmentData alloc] initWithFormatIdentifier:@"com.whatever.app" formatVersion:@"1.0" data:[NSData data]]; 
4

由於iOS的10,調整數據必須至少有一個字節。這是iOS 9的突破性變化,其中調整數據可能爲零。我已經在iOS 9和iOS 10上對此進行了測試以確認。

附加文檔:https://developer.apple.com/reference/photos/phcontenteditingoutput/1518684-adjustmentdata

PHContentEditingOutput* output = [[PHContentEditingOutput alloc] initWithContentEditingInput:self.input]; 
NSMutableData* adjustmentData = [NSMutableData data]; 
uint8_t byte = 1; 
[adjustmentData appendBytes:&byte length:1]; 
output.adjustmentData = [[PHAdjustmentData alloc] initWithFormatIdentifier:@"com.yourcompany.yourapp" formatVersion:@"1.0f" data:adjustmentData];