iOS 8+
Android 17+
TOCropViewController for iOS
uCrop for Android
Run tns plugin add nativescript-imagecropper
To use the image cropping module you must first require it.
var ImageCropper = require("nativescript-imagecropper").ImageCropper;
var camera = require("nativescript-camera");
// You might want to request camera permissions first
// check demo folder for sample implementation
camera.takePicture({width:300,height:300,keepAspectRatio:true})
.then((imageAsset) => {
let source = new imageSource.ImageSource();
source.fromAsset(imageAsset).then((source) => {
// now you have the image source
// pass it to the cropper
});
}).catch((err) => {
console.log("Error -> " + err.message);
});
show(ImageSource)
: Returns a cropped ImageSource
var imageCropper = new ImageCropper();
imageCropper.show(imageSource).then((args) => {
console.dir(args);
if(args.image !== null){
imageView.imageSource = args.image;
}
})
.catch(function(e){
console.dir(e);
});
show(ImageSource,Options)
: Returns a cropped and resized ImageSource
var imageCropper = new ImageCropper();
imageCropper.show(imageSource,{width:300,height:300}).then((args) => {
console.dir(args);
if(args.image !== null){
imageView.imageSource = args.image;
}
})
.catch(function(e){
console.dir(e);
});
Option | Type | Description |
---|---|---|
width | number | The width of the image you would like returned. |
height | number | The height of the image you would like returned. |
lockSquare | boolean | Pass this as true, to lock square aspect ratio on iOS, on android, this option doesn't make any difference. |
You can override library colors just specifying colors with the same names in your colors.xml file. For example:
<color name="ucrop_color_toolbar">#000000</color>
This will make toolbar color black if specified inside your App_Resources/Android/values/colors.xml
file.
(thanks to @chrispoket99 for mentioning this)
Argument | Type | Result(s) |
---|---|---|
response | string | Success Cancelled Error |
image | ImageSource | null if there was an error or was cancelledImageSource on success |