如何在iOS cordova项目中将目标C的资产URL返回到javascript?

时间:2014-07-21 07:03:07

标签: javascript ios objective-c cordova alassetslibrary

我正在为ios尝试一个cordova项目。 我的日志中有一个资产网址,我在我的代码中尝试使用该网址并且工作正常。 但是,我需要将asset-url作为从目标C到javascript的返回值。

- (void)saveImageDataToLibrary:(CDVInvokedUrlCommand*)command{
    self.callbackId = command.callbackId;
    NSData* imageData = [NSData dataFromBase64String:[command.arguments objectAtIndex:0]];

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

    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

    UIImage *viewImage = image;  // --- mine was made from drawing context
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    // Request to save the image to camera roll
    [library writeImageToSavedPhotosAlbum:[viewImage CGImage] orientation:(ALAssetOrientation)[viewImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
        if (error) {
            NSLog(@"error");
        } else {
        NSLog(@"url %@", assetURL);
        }
    }];
    [library release];
}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    // Was there an error?
    if (error != NULL)
    {
        // Show error message...
        NSLog(@"ERROR: %@",error);
        CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString:error.description];
        [self.webView stringByEvaluatingJavaScriptFromString:[result toErrorCallbackString: self.callbackId]];
    }
    else  // No errors
    {
        // Show message image successfully saved
        NSLog(@"IMAGE SAVED!");
        CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:@"Image saved"];
    [self.webView stringByEvaluatingJavaScriptFromString:[result toSuccessCallbackString: self.callbackId]];
    }
}

下面.... 这段代码给了我日志....

NSLog(@"url %@", assetURL);

我想将此assetURL作为我的javascript的返回值,我在这里调用此函数...

我已经在这里呆了一个星期......

帮助我解除这个......

Thnx提前.....

1 个答案:

答案 0 :(得分:0)

谢谢大家谁试过这个....我经过一些搜索后得到了.... 我在上面显示的代码中的NSLog行之后添加了代码......如下所示......

NSLog(@"url %@", assetURL);
        CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:[assetURL absoluteString]];
        [self.webView stringByEvaluatingJavaScriptFromString:[result toSuccessCallbackString: self.callbackId]];