如何在iOS cordova项目中从objective C返回一个资源url到javascript

How to return an asset url from objective C to javascript in iOS cordova project?

本文关键字:一个 资源 javascript url 返回 iOS cordova 项目 objective      更新时间:2023-09-26

我正在尝试ios的cordova项目。我在日志中有一个资产url,我尝试在代码中使用那个url,它工作得很好。但是,我需要资产url作为从objective 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提前.....

感谢所有尝试过....的经过一番搜索,我找到了....我在上面显示的代码中添加了NSLog行之后的代码………

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