nativescript-bitmap-factory
nativescript-bitmap-factory copied to clipboard
Error when trying to crop: "undefined is not an object" on iOS
Hi, I was attempting to extract regions from an image using crop(). I get the blow error from the bmp.insert line. This works on an android device, but not on iphone...
JS ERROR TypeError: undefined is not an object (evaluating 'this._nativeObject.size.width')
let bmp = BitmapFactory.create(500, 500);
bmp.dispose(function() {
bmp.drawLine("0,150", "300,75", '#0000ff');
let leftTop = "0,0";
let size = "50x50";
let croppedImage = bmp.crop(leftTop,size);
bmp.insert(croppedImage);
});
Many thanks!
Any luck with this? I am getting the same.
@jwall132 - I ended up doing this for IOS:
let croppedImg: ImageSource;
let nativeImg: UIImage = photo.ios;
let widthInPoints = nativeImg.size.width;
let widthInPixels = widthInPoints * nativeImg.scale;
let heightInPoints = nativeImg.size.height;
let heightInPixels = heightInPoints * nativeImg.scale;
var imageRef = CGImageCreateWithImageInRect(nativeImg.CGImage, CGRectMake(coords.x, coords.y, coords.w, coords.h));
let nativeCroppedImg = new UIImage(imageRef);
let imageData: NSData = UIImagePNGRepresentation(nativeCroppedImg);
croppedImg = ImageSourceFromData(imageData);
To flesh out @geoffbullen's answer, I got his code working by replacing ImageSourceFromData which doesn't seem to be a function I can find like so:
const imageData: any = UIImagePNGRepresentation(nativeCroppedImg);
croppedImg = UIImage.imageWithData(imageData);
const mutable2 = BitmapFactory.makeMutable(croppedImg);
const bmp2 = BitmapFactory.asBitmap(mutable2);