Skip to content

Instantly share code, notes, and snippets.

@nitrag
Created January 21, 2016 21:59
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 nitrag/3207abe888089db52f47 to your computer and use it in GitHub Desktop.
Save nitrag/3207abe888089db52f47 to your computer and use it in GitHub Desktop.
MaxImage.js
exports.createView = function(args) {
var imageView = Ti.UI.createImageView(args);
imageView.setMaxImage = function(url, maxWidth, maxHeight){
var preview = Ti.UI.createImageView();
Ti.API.info("ImageView's width = " + imageView.getWidth());
preview.addEventListener('load', function(e){
Ti.API.info("Loaded preview, resizing...");
var ratioX = imageView.getWidth() / image.width(); //hardcoded to imageView's max width since this is static to my needs, change for full functionality
var ratioY = imageView.getWidth() / image.height(); //hardcoded for a square image for my needs, change to maxHeight for your needs
var ratio = Math.min(ratioX, ratioY);
var w = imageWidth * ratio;
var h = imageHeight * ratio;
imageView.setWidth(w);
imageView.setHeight(h);
imageView.image = url;
});
preview.image = url;
};
return imageView;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment