捕获并将使用相机拍摄的照片存储到本地数据库/ PhoneGap/Cordova/iOS中

Jér*_*ôme 15 camera local-storage ios cordova phonegap-build

我目前正在使用Phonegap/Cordova和jQuerymobile为iOS构建应用程序.我们的想法是用相机拍照并存储拍摄的图像以备将来使用.我想将路径/文件名存储到我的本地数据库中,并将图片文件移动到iPhone中的持久位置.

有人可以给我一个例子吗?

Jér*_*ôme 36

好的,这是解决方案.

  • 在Html文件中

  • 我有一个图像标签,用于显示相机拍摄的照片:

  • 我有一个按钮,可以运行拍摄照片的功能:拍摄照片

  • 捕获照片的功能是(拍摄照片时,'smallImage'id的scr填充了照片的路径)

    function capturePhoto() {
    // Take picture using device camera and retrieve image as base64-encoded string
        navigator.camera.getPicture(onPhotoDataSuccess, onFail, { quality: 50 });
    }
    
    //Callback function when the picture has been successfully taken
    function onPhotoDataSuccess(imageData) {                
        // Get image handle
        var smallImage = document.getElementById('smallImage');
    
        // Unhide image elements
        smallImage.style.display = 'block';
        smallImage.src = imageData;
    }
    
    //Callback function when the picture has not been successfully taken
    function onFail(message) {
        alert('Failed to load picture because: ' + message);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 现在我想将图片移动到永久文件夹中,然后将链接保存到我的数据库中:

    function movePic(file){ 
        window.resolveLocalFileSystemURI(file, resolveOnSuccess, resOnError); 
    } 
    
    //Callback function when the file system uri has been resolved
    function resolveOnSuccess(entry){ 
        var d = new Date();
        var n = d.getTime();
        //new file name
        var newFileName = n + ".jpg";
        var myFolderApp = "EasyPacking";
    
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSys) {      
        //The folder is created if doesn't exist
        fileSys.root.getDirectory( myFolderApp,
                        {create:true, exclusive: false},
                        function(directory) {
                            entry.moveTo(directory, newFileName,  successMove, resOnError);
                        },
                        resOnError);
                        },
        resOnError);
    }
    
    //Callback function when the file has been moved successfully - inserting the complete path
    function successMove(entry) {
        //I do my insert with "entry.fullPath" as for the path
    }
    
    function resOnError(error) {
        alert(error.code);
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 我的文件已保存在数据库中以显示它,我将"file://"放在包含图像src的行的前面

希望这有帮助.J.

PS: - 非常感谢Simon Mac Donald(http://hi.im/simonmacdonald)关于googledocs的帖子.

  • 你在哪里调用这个函数movePic(文件)? (10认同)
  • 我唯一不理解的是你调用movePic函数的地方 (4认同)

sam*_*ass -3

有3个步骤:

  1. 获取照片。Apple在 iPhone 开发网站上提供了一个很好的示例
  2. 获取您的文档目录,如下所示:
    
    NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(
                NSDocumentDirectory,
                NSUserDomainMask,
                YES);
    NSString *docDir = [arrayPaths objectAtIndex:0];
    
    Run Code Online (Sandbox Code Playgroud)
  3. 最后,将步骤 1 中获得的 UIImage 存储到磁盘:
    
    NSString *filename = @"mypic.png";
    NSString *fullpath = [docDir stringByAppendingPathComponent:filename];
    [UIImagePNGRepresentation(image) writeToFile:fullpath atomically:YES];
    
    Run Code Online (Sandbox Code Playgroud)