2016-04-13 6 views
0

Ich versuche, ein Base64-Bild auf Kamera zu speichern und die URL für das gespeicherte Bild zurückgeben. Der Code funktioniert soweit, dass es mir gelungen ist, in die Kamera zu speichern, aber ich sehe einen Fehler und es wird keine URL zurückgegeben. Der Fehler ist:Speichern Sie das Foto in die Kamera und geben Sie die URL

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

Mein Code ist:

- (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]; 
} 

Antwort

0

Versuchen zu verwenden: PHContentEditingOutput

Das Objekt hat eine Eigenschaft namens: renderedContentURL

verwenden, die um die entsprechende URL Ihres PHAsset zu erhalten.

So die URL zu erhalten, sollten Sie den Code wie folgt aussehen:

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

NSURL *myPHAssetURL = [contentEditingOutput renderedContentURL]; 
Verwandte Themen