2
votes

I am developing a mobile (iphone/android) application using appcelerator titanium (sdk 1.6.2). At a certain point in the app the user choses an image that should be shown in an imageView, base64 encoded, then uploaded to my server. The problem is the success event of the photo gallery returns the selected image as a blob object and the Titanium.Utils.base64encode method only accepts string values! Is there any way to convert Titanium.Blob objects to strings?

Here is the code snippet:

var imageView = Titanium.UI.createImageView({
height:200,
width:200,
top:20,
left:10,
backgroundColor:'#999'
});

Titanium.Media.openPhotoGallery({

success:function(event)
{
    var cropRect = event.cropRect;
    var image = event.media;//blob object

    // set image view
    Ti.API.debug('Our type was: '+event.mediaType);
    if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
    {
        imageView.image = image;// this works
        var imgStr=Ti.Utils.base64encode(image);// doesn't work because 'image' has to be a string!, but how?
    }
    else
    {

    }

    Titanium.API.info('PHOTO GALLERY SUCCESS cropRect.x ' + cropRect.x + ' cropRect.y ' + cropRect.y  + ' cropRect.height ' + cropRect.height + ' cropRect.width ' + cropRect.width);

},
allowEditing:true,
popoverView:popoverView,
arrowDirection:arrowDirection,
mediaTypes:[Ti.Media.MEDIA_TYPE_VIDEO,Ti.Media.MEDIA_TYPE_PHOTO]
});

Thank you,

3

3 Answers

3
votes
var imgStr=Ti.Utils.base64encode(image.toString());

.toString() converts something to a string representation

1
votes

This worked for me.

var image = event.media;
var imgStr = Ti.Utils.base64encode(image).toString();
0
votes

i just posted some code for a module to do this conversion, I know a patch is coming from appcelerator, but the module might be useful to you now.

Clearly Innovative Thoughts - Titanium Appcelerator Quickie: base64encode iOS Module