Skip to content

Instantly share code, notes, and snippets.

@MotiurRahman
Created June 5, 2018 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MotiurRahman/f5f2392af6f010d22c0d199fa9545124 to your computer and use it in GitHub Desktop.
Save MotiurRahman/f5f2392af6f010d22c0d199fa9545124 to your computer and use it in GitHub Desktop.
Memory Issue
var win1 = Ti.UI.createWindow({
//backgroundColor : 'red',
barColor : "#800000",
layout : "vertical"
});
// Create a Button.
var btn1 = Ti.UI.createButton({
title : 'Camera Open',
height : 100,
width : 100,
backgroundImage : "motiur.jpg",
top : 50
});
var ImageFactory = require('ti.imagefactory');
var image1 = Ti.UI.createImageView({
top : 20,
height : 'auto',
width : 'auto'
});
var image2 = Ti.UI.createImageView({
top : 20,
height : 'auto',
width : 'auto'
});
function takePicture() {
Ti.Media.showCamera({
//function to call upon successful load of the gallery
success : function(e) {
Ti.API.info("TakeTransactionImage: Success.");
if (e.mediaType === Titanium.Media.MEDIA_TYPE_PHOTO) {
Ti.API.info("TakeTransactionImage: Success. Was a photo.");
//e.media represents the photo or video
//sImageName = GetRandomFileName();
var image = e.media;
image1.setImage(image);
win1.add(image1);
Ti.API.info('TakeTransactionImage(1)');
var wid = (image1.toBlob().width) / 8;
var hei = (image1.toBlob().height) / 8;
//RESIZE IMAGE - HERE IS DIVIDING THE IMAGE DIMENSIONS BY 4
Ti.API.info('TakeTransactionImage(2) image1.width=' + wid + "\n" + "Image1.height:" + hei);
Ti.API.info('ImageFactory.QUALITY_MEDIUM = ' + ImageFactory.QUALITY_MEDIUM);
//THIS IS THE LINE THAT TRIGGERS THE OUT OF MEMORY ERROR
var newImage = ImageFactory.imageAsResized(image, {
width : wid,
height : hei
});
image2.setImage(newImage);
win1.add(image2);
//var f = "";
var filename = new Date().getTime() + "-ea.jpg";
var f = Ti.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename);
f.write(newImage);
Ti.API.info('TakeTransactionImage(4) f.nativePath = ' + f.nativePath);
}
image1.remove(image);
image2.remove(newImage);
image=null;
newImage=null;
},
cancel : function(e) {
callback(e, null);
},
error : function(e) {
callback(e, null);
},
saveToPhotoGallery : false, // save our media to the gallery
//mediaTypes : [type]
});
}
btn1.addEventListener('click', function(e) {
if (!Ti.Media.hasCameraPermissions()) {
// request permissions to capture media
Ti.Media.requestCameraPermissions(function(e) {
// success! we can capture media!
if (e.success) {
takePicture();
// oops! could not obtain required permissions...
} else {
Ti.API.error('could not obtain camera permissions!');
}
});
} else {
// yay! we already have permissions!
takePicture();
}
});
win1.add(btn1);
win1.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment