【问题标题】:Save photo to camera roll and return url将照片保存到相机胶卷并返回 url
【发布时间】:2016-04-13 16:18:26
【问题描述】:

我正在尝试将 base64 图像保存到相机胶卷并返回已保存图像的 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];
}

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    尝试使用: PHContentEditingOutput

    该对象有一个名为:renderedContentURL

    的属性

    使用它来获取您的 PHAsset 的适当 URL。

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

    PHContentEditingOutput *contentEditingOutput = [[PHContentEditingOutput alloc] initWithContentEditingInput:YOUR_PHCONTENTEDITING_INPUT];
    
    NSURL *myPHAssetURL = [contentEditingOutput renderedContentURL];
    

    【讨论】:

      猜你喜欢
      • 2014-09-04
      • 2013-07-18
      • 2012-07-22
      • 2013-04-03
      • 2012-05-01
      • 2014-04-21
      • 2018-09-22
      • 2013-07-22
      • 1970-01-01
      相关资源
      最近更新 更多