Skip to content

Instantly share code, notes, and snippets.

@hansemannn
Created March 14, 2019 11:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hansemannn/723b83826d53158dad777ae827685773 to your computer and use it in GitHub Desktop.
Save hansemannn/723b83826d53158dad777ae827685773 to your computer and use it in GitHub Desktop.
An example of cropping images cross-platform on Titanium.
/**
* Crops a given image URL to an optional aspect ratop (square by default).
*
* Example:
*
* const image = await ImageCroppingManager.crop(myImageURL);
*
*/
export default class ImageCroppingManager {
/**
* Performs the image cropping.
* @param {String|Ti.Blob|Ti.File} imageURL The image URL to use.
* @param {Object|Number} aspectRatio The aspect ratio to use (defaults to square)
*/
static async crop(imageURL, aspectRatio = { x: 1, y: 1 }) {
return new Promise((resolve, reject) => {
if (OS_IOS) {
const ImageCrop = require('ti.imagecrop');
ImageCrop.addEventListener('done', event => {
if (event.cancel) {
reject();
return;
}
resolve(evemt.image);
});
ImageCrop.showCropDialog({ image, aspectRatio, });
} else {
// TODO: Implement on Android
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment