Skip to content

Instantly share code, notes, and snippets.

@rpavez
Created March 5, 2015 14:42
Show Gist options
  • Save rpavez/45ec3a6394d9a41baad8 to your computer and use it in GitHub Desktop.
Save rpavez/45ec3a6394d9a41baad8 to your computer and use it in GitHub Desktop.
Ti.ImageFactory Lib Tests
var ImageFactory = require('ti.imagefactory');
Ti.API.log("Image factory loaded");
Ti.App.resizeKeepAspectRatioPercentage = function(blob, percentage) {
try {
var imageTemp = Ti.UI.createImageView({
image : blob,
height : 'auto',
width : 'auto'
});
imageHeight = parseInt(imageTemp.toBlob().height);
imageWidth = parseInt(imageTemp.toBlob().width);
imageTemp = null;
//Ti.API.debug(imageHeight);
//Ti.API.debug(imageWidth);
if (imageWidth < imageHeight) {
if (imageHeight < 1024) {
//Ti.API.error('entre aqui h');
return blob;
}
} else {
if (imageWidth < 1024) {
//Ti.API.error('entre aqui w');
return blob;
}
}
// only run this function if suitable values have been entered
if (imageWidth <= 0 || imageHeight <= 0 || percentage <= 0)
return blob;
var w = imageWidth * (percentage / 100);
var h = imageHeight * (percentage / 100);
//Ti.API.debug(w);
//Ti.API.debug(h);
newBlob = ImageFactory.imageAsResized(blob, {
width : w,
height : h
});
compressedBlob = ImageFactory.compress(newBlob, 0.50);
return compressedBlob;
} catch(err) {
Ti.API.error(err);
Ti.API.warn('ERROR DEL RESIZE');
return blob;
}
};
Ti.App.resizeKeepAspectRatioValue = function(blob, value) {
try {
var imageTemp = Ti.UI.createImageView({
image : blob,
height : 'auto',
width : 'auto'
});
imageHeight = parseInt(imageTemp.toBlob().height);
imageWidth = parseInt(imageTemp.toBlob().width);
imageTemp = null;
var newW;
var newH;
var ratio;
if (imageWidth < imageHeight) {
if (imageHeight < value) {
return blob;
}
newW = Math.ceil((imageWidth / imageHeight) * value);
newH = value;
} else {
if (imageWidth < value) {
return blob;
}
newW = value;
newH = Math.ceil((imageHeight / imageWidth) * value);
}
if (imageWidth <= 0 || imageHeight <= 0 || value <= 0)
return blob;
newBlob = ImageFactory.imageAsResized(blob, {
width : newW,
height : newH
});
compressedBlob = ImageFactory.compress(newBlob, 0.50);
return compressedBlob;
} catch(err) {
Ti.API.error(err);
Ti.API.warn('Resize failed');
return blob;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment