2016-04-13 11 views
0

カメラロールにbase64画像を保存し、保存した画像のURLを返そうとしています。私がカメラロールに保存するのに成功した限り、コードは動作しますが、エラーが表示され、URLは返されません。エラーは次のとおりです。写真をカメラロールに保存してURLを返す

Error Domain=NSCocoaErrorDomain Code=-1 "(null)" 

私のコードは次のとおりです。使用する

- (void)saveImageDataToLibrary:(CDVInvokedUrlCommand*)command 
{ 

    __block CDVPluginResult* result = nil; 

    NSData* imageData = [NSData dataFromBase64String:[command.arguments objectAtIndex:0]]; 

    UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease]; 

    __block PHObjectPlaceholder *placeholderAsset = nil; 

    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ 

     PHAssetChangeRequest *newAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image]; 

     placeholderAsset = newAssetRequest.placeholderForCreatedAsset; 

    } completionHandler:^(BOOL success, NSError *error) { 
     if(success){ 
      NSLog(@"worked"); 
      PHAsset *asset = [self getAssetFromlocalIdentifier:placeholderAsset.localIdentifier]; 

      PHContentEditingInputRequestOptions *options = [[PHContentEditingInputRequestOptions alloc] init]; 
      options.networkAccessAllowed = YES; //download asset metadata from iCloud if needed 

      [asset requestContentEditingInputWithOptions:options 
       completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) { 
       NSURL *assetURL = contentEditingInput.fullSizeImageURL; 
       NSString* url = [assetURL absoluteString]; 
       NSLog(@"our result is: %@", url); 

       result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:url]; 
       [self invokeCallback:command withResult:result]; 

      }]; 

     } else { 
      NSLog(@"Error: %@", error); 
      result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:error.description]; 
      [self invokeCallback:command withResult:result]; 
     } 
    }]; 

} 

- (void) invokeCallback:(CDVInvokedUrlCommand *)command withResult:(CDVPluginResult *)result { 
    [self.commandDelegate sendPluginResult:result callbackId:command.callbackId]; 
} 

答えて

0

試してみてください。 PHContentEditingOutput

というプロパティを持つオブジェクト:renderedContentURL

使用することをあなたのPHAssetの適切なURLを取得します。

ので、URLを取得するには、あなたのコードは次のようになります。

PHContentEditingOutput *contentEditingOutput = [[PHContentEditingOutput alloc] initWithContentEditingInput:YOUR_PHCONTENTEDITING_INPUT]; 

NSURL *myPHAssetURL = [contentEditingOutput renderedContentURL]; 
関連する問題