-
-
Save himalay/f425809c64f77c0e61f0 to your computer and use it in GitHub Desktop.
Revisions
-
FokkeZB created this gist
May 2, 2013 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,27 @@ Often I need to display a user-provided picture in an ImageView in such a way that the whole ImageView is filled with as much of the picture possible. This is how I do it: ``` var image = require('image'); Ti.Media.showCamera({ mediaTypes: [Ti.Media.MEDIA_TYPE_PHOTO], success: function (e) { myImageView.image = image.crop(e.media, myImageView.rect); } }); ``` Take a look at the next `image.js` file to learn how it works. ## Params * `blob`: The `Ti.Blob` containing the image. * `options`: Either an `object` (or [Dimension](http://docs.appcelerator.com/titanium/latest/#!/api/Dimension)) containing the target `width` and `height` and the other options listed below, or a Number representing only the `width`. * `height`: The target `height`. ## Options * `width`: See above. * `height`: See above. * `pixels`: Set to `FALSE` to disable converting target dimensions to actual pixels needed for either Retina or Android variable DPI. * `fix`: Set to `FALSE` to disable the workaround for bug [TIMOB-4865](https://jira.appcelerator.org/browse/TIMOB-4865). 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,95 @@ var _factor; function crop(blob, options, height) { if (typeof options !== 'object') { options = { width: options, height: height } } if (!blob.width || !blob.width) { return blob; } // https://jira.appcelerator.org/browse/TIMOB-4865 if (options.fix !== false) { blob = blob.imageAsResized(blob.width, blob.height); } if (options.pixels !== false) { options = pixels(options); } if (options.width && options.height) { var blob_ratio = blob.width / blob.height; var ratio = options.width / options.height; if (blob_ratio !== ratio) { // Cut left and right if (blob_ratio > ratio) { blob = blob.imageAsCropped({ width: Math.round(blob.height * ratio), }); } // Cut top and bottom else { blob = blob.imageAsCropped({ height: Math.round(blob.width / ratio) }); } } if (blob.width !== options.width || blob.height !== options.height) { blob = blob.imageAsResized(options.width, options.height); } return blob } else { return blob.imageAsCropped(options); } } function pixels(dimension) { if (typeof dimension === 'number') { return dimension * pixelFactor(); } if (dimension.width) { dimension.width = dimension.width * pixelFactor(); } if (dimension.height) { dimension.height = dimension.height * pixelFactor(); } return dimension; } function pixelFactor() { if (!_factor) { _factor = 1; if (Ti.Platform.name === 'iPhone OS') { if (Ti.Platform.displayCaps.density === 'high') { _factor = 2; } } else if (Ti.Platform.name === 'android') { _factor = (Ti.Platform.displayCaps.dpi / 160); } } return _factor; } exports.crop = crop; exports.pixels = pixels; exports.pixelFactor = pixelFactor;