nativescript-bitmap-factory icon indicating copy to clipboard operation
nativescript-bitmap-factory copied to clipboard

Error when trying to crop: "undefined is not an object" on iOS

Open geoffbullen opened this issue 6 years ago • 3 comments

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!

geoffbullen avatar Apr 12 '18 04:04 geoffbullen

Any luck with this? I am getting the same.

jackwaller avatar Apr 20 '18 11:04 jackwaller

@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);

geoffbullen avatar Apr 22 '18 23:04 geoffbullen

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);

DanLatimer avatar Nov 15 '18 23:11 DanLatimer