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

.rotate() function seems to return a blank image

Open mlewis-everley opened this issue 7 years ago • 3 comments

So I am using the following code in a fairly simple view:

import * as Enums from "ui/enums";
import * as BitmapFactory from "nativescript-bitmap-factory";

let temp_path = global.temp_path;
let image = ImageSource.fromFile(temp_path);

let mutable_opts: BitmapFactory.IMakeMutableOptions = {
    disposeCurrent: true,
    temp: {
        stradegy: BitmapFactory.TempFileStradegy.CacheDir,
    }
};

BitmapFactory
    .asBitmap(BitmapFactory.makeMutable(
        image,
        mutable_opts
    )).dispose(function(b) {
        var rotate = 90;
        b = b.rotate(rotate);
                        
        image = b.toImageSource();

        image.saveToFile(
            temp_path,
            Enums.ImageFormat.jpeg
        );
    });

FrameModule
    .topmost()
    .navigate({
        moduleName: 'views/review/review',
        clearHistory: true
    });

The intention is to rotate the image and save it to a temp folder (temp_path) then we display it on a separate screen (views/review/review).

When I review the image I just get a blank screen (it looks like the image container is set but is not being filled with a rotated image).

I am using similar code, but with b.resize() instead of b.rotate() and that seems to work as expected.

I have recently upgraded to tns 2.5.0 and typescript 2.0.3 and also upgraded this module to 1.7.0, I am wondering if this might be related?

Many thanks for any assistance,

Mo

mlewis-everley avatar Apr 12 '17 09:04 mlewis-everley

I get this too

geoffbullen avatar Mar 21 '19 00:03 geoffbullen

In BitmapFactory.ios.js replace rotate function with this:

// [INTERNAL] _rotate()
iOSImage.prototype._rotate = function (degrees) {
    radians = degrees * Math.PI / 180.0;

    var oldImg = this._nativeObject;

    try {
        UIGraphicsBeginImageContext(oldImg.size);

        var context = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(context, oldImg.size.width / 2.0, oldImg.size.height / 2.0);
        CGContextRotateCTM(context, radians);

        oldImg.drawInRect(CGRectMake(-oldImg.size.width / 2.0, -oldImg.size.height / 2.0, oldImg.size.width, oldImg.size.height));

        return UIGraphicsGetImageFromCurrentImageContext();
    }
    finally {
        UIGraphicsEndImageContext();
    }
};

adfdev avatar Aug 22 '19 12:08 adfdev

Thanks for the fix @adfdev

geoffbullen avatar Dec 17 '19 01:12 geoffbullen