Using PhoneGap, How to get base64 image data of the photo chosen from photo library in iPhone

前端 未结 6 1459
无人及你
无人及你 2021-01-12 08:58

Using PhoneGap(Cordova), Am trying to get base64 image data of the photo chosen from the photo library.

I could do that.. when the photo is captured from camera, Wi

6条回答
  •  生来不讨喜
    2021-01-12 09:41

    Try this out.

    function getPhoto(source) {
        // Retrieve image file location from specified source
        navigator.camera.getPicture(onPhotoURISuccess, onFail, { quality: 50,
            destinationType: destinationType.FILE_URI,
            sourceType: source });
    } 
    
    function onPhotoURISuccess(imageURI) {
    
        window.resolveLocalFileSystemURI(imageURI, gotFileEntry, failSystem);
    
    }
    function onFail(message) {
        alert('Failed because: ' + message);
    }
    var gotFileEntry = function(fileEntry) {
       // alert(fileEntry.fullPath);
        console.log("got image file entry: " +  fileEntry.fullPath);
    //convert all file to base64 formats
        fileEntry.file( function(file) {
    //uncomment the code if you need to check image size
           //if(file.size>(1049576/2))
          // {
               //alert('File size exceeds 500kb');
              // return false;
          // }
            var reader = new FileReader();
            reader.onloadend = function(evt) {
                console.log("Read complete!");
                $('yourimageidtoshow').attr('src',  evt.target.result);
            };
            reader.readAsDataURL(file);
        }, failFile);
    };
    var failSystem=function(){
        console.log('failed');
    
    }
    var failFile=function(){
    
        console.log('failed');
        throw "toobig";
    };
    

提交回复
热议问题