unable to set image in paint view in titanium?

You must Login before you can answer or comment on any questions.

Hi, i have created drawing using paint and i saved it as a image and insert it in database and then placed as a image in paint image but it gives folloawing error when i set image in a paintview

CGContextSaveGState: invalid context 0x0

I have used following code to setimage in paintview

var paintView = Titanium.Painter.createPaintView({ top: 0, left: 0, right: 0,
bottom: 0, strokeColor: '#0f0', //strokeColor: '#ffffff', strokeAlpha: 255, strokeWidth: 10, });

paintView.addEventListener('touchend',function(e) {

          var paintimage = paintView.toImage();
          imagedb.addtempImage(paintimage);


    });

exports.addtempImage = function(imageitem) {
  var  db = Ti.Database.open('db');
  db.execute('INSERT INTO Tempdrawingimage (tempimage) values (?)', imageitem);
  var dbrows =db.execute('SELECT tempimage FROM Tempdrawingimage');
   Ti.API.info( dbrows.getRowCount());
   dbrows.close();
  db.close();
};

sliderbar.addEventListener('swipe',function(e) {
var tempimg = imagedb.gettempImage(Ti.App.myglobaltempimage); paintView.clear(); paintView.image = tempimg; }

    });

2 Answers

Accepted Answer

I think the best solution is to actually save the image to the filesystem. Here are a couple of functions I've created that do just that:

var getPaintImage = function(){
    var filename = "savedPainting.jpg";
    var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
    if(!file.exists()){
        return 'File not found.';
    } else {
        return Titanium.Filesystem.applicationDataDirectory+filename;
    }
},
 
var savePaintImage = function(imageBlob){
    var filename = "savedPainting.jpg";
    var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,filename);
    file.write(cover);
}
So, then, you would do the following to save the image:
var savedPainting = paintView.toImage();
savePaintImage(savedPainting);
You could, of course, add a parameter to the save and retrieve functions allowing you to use a custom filename as well.

Hope that helps!

— answered 10 months ago by Beejay Morgan
answer permalink
2 Comments
  • Sorry, I should point out that the getPaintImage function returns the path to the image file, and not the image data itself. You can then set this image path as the image property in your paint view.

    — commented 10 months ago by Beejay Morgan

  • Ugh... I should really proofread better. The last line should be as follows:

    file.write(imageBlob);
    So cover should be imageBlob

    Apologies.

    — commented 10 months ago by Beejay Morgan

you cannot save that image to the database... unless you convert it to a string representation

Your Answer

Think you can help? Login to answer this question!