Created
March 14, 2019 11:04
-
-
Save hansemannn/723b83826d53158dad777ae827685773 to your computer and use it in GitHub Desktop.
An example of cropping images cross-platform on Titanium.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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