Quantcast
Channel: Appcelerator Developer Center Q&A Unanswered Questions 20
Viewing all articles
Browse latest Browse all 8068

How to convert Image to Blob and convert it back to image?

$
0
0

var win = Titanium.UI.createWindow({
    title:"Using the Camera and Video",
    backgroundColor:"#FFFFFF"
});
 
var button = Titanium.UI.createButton({
    title:"Use camera",
    width:180,
    height:48,
    bottom: 12,
    zIndex:2
})
 
button.addEventListener("click", function(e){
    //Open the photo gallery
    Titanium.Media.showCamera({
        //function to call upon successful load of the gallery
        success:function(e){
            if(e.mediaType === Titanium.Media.MEDIA_TYPE_PHOTO){
                //e.media represents the photo or video
                var imageView = Titanium.UI.createImageView({
                    image:e.media,
                    width:320,
                    height:480,
                    top:12,
                    zIndex:1
                });
                win.add(imageView);
 
 
// This method doesn't work fine, So I want to use BLOB method instead      
        var imageAsTaken = Ti.UI.createImageView({image:e.media});
        var imageFile = imageAsTaken.toImage();
 //not working method end
 
        var xhr = Titanium.Network.createHTTPClient();      
            xhr.onload = function() {
                var parseResponse = JSON.parse(xhr.responseText);
                    alert(parseResponse);
 
            };
            xhr.onerror = function() {
                alert('ERROR response: '+ xhr.responseText + '   status code:' + xhr.status);
            };
            xhr.open('POST', 'https://api.parse.com/1/files/pic.jpg');
            xhr.setRequestHeader('X-Parse-Application-Id', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
            xhr.setRequestHeader('X-Parse-REST-API-Key', 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx');
            xhr.setRequestHeader('Content-Type', 'image/jpeg');
            xhr.send(imageFile);    
 
            }
 
        },
        error:function(e){
            alert("There was an error");
        },
        cancel:function(e){
            alert("The event was cancelled");
        },
        //Allow editing of media before success
        allowEditing:true,
        saveToPhotoGallery:true,
        //Media types to allow
        mediaTypes:[Titanium.Media.MEDIA_TYPE_PHOTO],
 
    });
})
 
Titanium.App.addEventListener('camera_button', function(){
    Titanium.Media.takePicture();
});
 
win.add(button);
 
win.open();
By using this method I can upload to Parse but no way to get it back. In order to simplify the thing I wanted to use BLOB method to convert Image to String so that I can store it in a simple way.

Any suggestion are appreciated!!


Viewing all articles
Browse latest Browse all 8068

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>