将照片保存到相机胶卷并返回网址

时间:2016-04-13 16:18:26

标签: ios objective-c

我正在尝试将base64图像保存到相机胶卷并返回已保存图像的网址。代码工作,因为我已成功保存到相机卷,但我看到一个错误,没有返回任何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];
}

1 个答案:

答案 0 :(得分:0)

尝试使用: 的 PHContentEditingOutput

该对象有一个名为: renderedContentURL

的属性

使用它来获取PHAsset的相应URL。

因此,要获取URL,您的代码应如下所示:

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

NSURL *myPHAssetURL = [contentEditingOutput renderedContentURL];