ios - 如何使用 phonegap 应用程序获取所选图像的 URL

标签 ios objective-c cordova

我正在使用 camera.getPicture() 方法打开照片库并让用户选择图像。这是我的引用

http://docs.phonegap.com/en/2.9.0/cordova_camera_camera.md.html#Camera

我们可以通过函数 onPhotoURISuccess(imageURI) 获取 base64 字符串形式的图像或获取其 URI 并在我们的应用程序中使用。 我选择 URI 是因为它对我来说很容易处理。我卡住了,因为图像总是以 jpeg 格式返回,甚至无法测量图像的文件大小。我通过调试跟踪到URI,它总是类似于下面

file://localhost/Users/user/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/B50E3AD6-74F8-43F6-9F91-F28D2B06DF62/tmp/cdv_photo_116.jpg

它正在尝试将图像保存在本地并拍摄任何扩展名为 jpg 的图像。 我想知道如何解决这个问题。有没有人知道我们如何通过这种方法获取图像的 URL?

最佳答案

因为我没有得到答案,所以我继续挖掘 phoneGap 文档并最终得到了答案。以下是我的代码示例。请注意,文件扩展名识别方法取自 here

我的 html(在 phoneGap 应用程序中)页面中有一个按钮控件,如下所示 任何人都可以引用 here 中的 phoneGap 文档.太有用了,我用过2.9.0版本

<button onclick="getPhoto(Camera.pictureSource.PHOTOLIBRARY);">From Photo Library</button>

在我的 javascript 中,我有如下函数 getPhoto()

function getPhoto(source)
    {        
        // Retrieve image file location from specified source
        navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 100,
                                    destinationType: navigator.camera.DestinationType.NATIVE_URI, sourceType: source });
    }

一旦选择了照片,就会调用该代表

   function onPhotoURISuccess(imageURI) {
      // Get image handle
      var largeImage = document.getElementById('largeImage');

      // Unhide image elements
      largeImage.style.display = 'block';

      // Show the captured photo
      // The inline CSS rules are used to resize the image
      largeImage.src = imageURI;

      checkFileExtention(imageURI);
    }

checkFileExtention()函数异步调用native端的文件扩展名检查方法。为了简单起见,我只贴出应用逻辑部分

//functions checks the type of the image passed
- (NSString *)contentTypeForImageData:(NSString *)imageUrl
{
    NSURL *imageURL = [NSURL URLWithString:imageUrl];
    NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
    
    uint8_t c;
    [imageData getBytes:&c length:1];
    
    switch (c)
    {
        case 0xFF:
            return @"image/jpeg";
        case 0x89:
            return @"image/png";
        case 0x47:
            return @"image/gif";
        case 0x49:
        case 0x4D:
            return @"image/tiff";
        case 0x42:
            return @"@image/bmp";
    }
    return nil;
}

当获取 FILE_URI 时,它会在本地保存图像并发送如下所示的 URI file://localhost/Users/user/Library/Application%20Support/iPhone%20Simulator/6.0/Applications/B50E3AD6-74F8-43F6-9F91-F28D2B06DF62/tmp/cdv_photo_116.jpg

显然,文件类型是隐藏的,因为它总是显示为 jpg

当使用 NATIVE_URI 时,它看起来像这样 assets-library://asset/asset.JPG?id=5CF16D20-9A80-483A-AC1C-9692123610B1&ext=JPG 请注意,上面的 uri 显示 JPG,因为图像是 JPG,否则显示所选图像的真实文件扩展名。

最终,如果将 native uri 发送到 contentTypeForImageData 函数时,它会给我正确的输出

关于ios - 如何使用 phonegap 应用程序获取所选图像的 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17859731/

相关文章:

iOS 将照片保存到相机胶卷不会保留 EXIF/GPS 元数据

ios - 阻塞 for 循环,直到委托(delegate)方法被触发

ios - 是否可以在 uitableviewcontroller 或 uicollectionviewcontroller 中添加 UIView (Admob)

ios - 如何快速获取从图库中选择的图像路径

angular - 由于文件传输已弃用,因此无法下载文件

android - 用Cordova显示本地保存的图片

ios - connectionDidFinishLoading 中的 TableView 重新加载数据时出错(通过 json 解析)

ios - UITableViewCell 类中的预期声明

ios - Objective-C 类的 initWithDelegate

cordova - 如何从 Cordova Azure 验证应用程序查询 Azure Web API