mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Improve ImageAsset scaling (#5110)
* Do not depend on current device screen while calculating Image Asset size. * Scale the image asset to the exact requested size. * Process image assets natively, pass keepAspectRatio based on the stretch property and Asset options. * Fixed the splashscreen resource name as it cannot be read when containing a dot. * Updated the Image Asset scale and rotate logic based on the Native one. * Make the ImageAsset size more important than the Image decode size as its more specific. * Fixed tslint errors. * Added filePath support in the ImageAsset constructor for iOS in order to unify it with the Android implementation, support for relative files and file not found support errors. * Added unit tests for ImageAssets. * Added a sample app for UI testing of image-view with ImageAsset src. * chore: apply PR comments
This commit is contained in:
committed by
Alexander Djenkov
parent
27622d83ba
commit
a94ec9946f
Binary file not shown.
|
Before Width: | Height: | Size: 26 KiB |
BIN
tests/app/App_Resources/Android/drawable-nodpi/splashscreen.png
Normal file
BIN
tests/app/App_Resources/Android/drawable-nodpi/splashscreen.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 35 KiB |
BIN
tests/app/App_Resources/iOS/splashscreen.png
Normal file
BIN
tests/app/App_Resources/iOS/splashscreen.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 35 KiB |
@@ -1,10 +1,14 @@
|
||||
import * as imageSource from "tns-core-modules/image-source";
|
||||
import * as imageAssetModule from "tns-core-modules/image-asset";
|
||||
import * as fs from "tns-core-modules/file-system";
|
||||
import * as app from "tns-core-modules/application";
|
||||
import * as TKUnit from "../TKUnit";
|
||||
import * as platform from "tns-core-modules/platform";
|
||||
|
||||
const imagePath = "~/logo.png";
|
||||
const splashscreenPath = "~/splashscreen.png";
|
||||
const splashscreenWidth = 372;
|
||||
const splashscreenHeight = 218;
|
||||
const smallImagePath = "~/small-image.png";
|
||||
|
||||
export function testFromResource() {
|
||||
@@ -65,6 +69,96 @@ export function testFromFile() {
|
||||
TKUnit.assert(!fs.File.exists(path), "test.png not removed");
|
||||
}
|
||||
|
||||
export function testFromAssetFileNotFound(done) {
|
||||
let asset = new imageAssetModule.ImageAsset('invalidFile.png');
|
||||
asset.options = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
keepAspectRatio: true
|
||||
};
|
||||
|
||||
let img = imageSource.fromAsset(asset).then((source) => {
|
||||
done('Should not resolve with invalid file name.');
|
||||
}, (error) => {
|
||||
TKUnit.assertNotNull(error);
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
export function testFromAssetSimple(done) {
|
||||
let asset = new imageAssetModule.ImageAsset(splashscreenPath);
|
||||
asset.options = {
|
||||
width: 0,
|
||||
height: 0,
|
||||
keepAspectRatio: true
|
||||
};
|
||||
|
||||
let img = imageSource.fromAsset(asset).then((source) => {
|
||||
TKUnit.assertEqual(source.width, splashscreenWidth);
|
||||
TKUnit.assertEqual(source.height, splashscreenHeight);
|
||||
done();
|
||||
}, (error) => {
|
||||
done(error);
|
||||
});
|
||||
}
|
||||
|
||||
export function testFromAssetWithScaling(done) {
|
||||
let asset = new imageAssetModule.ImageAsset(splashscreenPath);
|
||||
let scaleWidth = 10;
|
||||
let scaleHeight = 11;
|
||||
asset.options = {
|
||||
width: scaleWidth,
|
||||
height: scaleHeight,
|
||||
keepAspectRatio: false
|
||||
};
|
||||
|
||||
let img = imageSource.fromAsset(asset).then((source) => {
|
||||
TKUnit.assertEqual(source.width, scaleWidth);
|
||||
TKUnit.assertEqual(source.height, scaleHeight);
|
||||
done();
|
||||
}, (error) => {
|
||||
done(error);
|
||||
});
|
||||
}
|
||||
|
||||
export function testFromAssetWithScalingAndAspectRatio(done) {
|
||||
let asset = new imageAssetModule.ImageAsset(splashscreenPath);
|
||||
let scaleWidth = 10;
|
||||
let scaleHeight = 11;
|
||||
asset.options = {
|
||||
width: scaleWidth,
|
||||
height: scaleHeight,
|
||||
keepAspectRatio: true
|
||||
};
|
||||
|
||||
let img = imageSource.fromAsset(asset).then((source) => {
|
||||
TKUnit.assertEqual(source.width, scaleWidth);
|
||||
TKUnit.assertEqual(source.height, 5);
|
||||
done();
|
||||
}, (error) => {
|
||||
done(error);
|
||||
});
|
||||
}
|
||||
|
||||
export function testFromAssetWithBiggerScaling(done) {
|
||||
let asset = new imageAssetModule.ImageAsset(splashscreenPath);
|
||||
let scaleWidth = 600;
|
||||
let scaleHeight = 600;
|
||||
asset.options = {
|
||||
width: scaleWidth,
|
||||
height: scaleHeight,
|
||||
keepAspectRatio: false
|
||||
};
|
||||
|
||||
let img = imageSource.fromAsset(asset).then((source) => {
|
||||
TKUnit.assertEqual(source.width, scaleWidth);
|
||||
TKUnit.assertEqual(source.height, scaleHeight);
|
||||
done();
|
||||
}, (error) => {
|
||||
done(error);
|
||||
});
|
||||
}
|
||||
|
||||
export function testNativeFields() {
|
||||
const img = imageSource.fromFile(imagePath);
|
||||
if (app.android) {
|
||||
|
||||
BIN
tests/app/splashscreen.png
Normal file
BIN
tests/app/splashscreen.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 35 KiB |
@@ -138,7 +138,8 @@ export const test_SettingImageSrcToDataURI_async = function (done) {
|
||||
|
||||
export function test_imageSourceNotResetAfterCreateUI() {
|
||||
let image = new ImageModule.Image();
|
||||
let imageSource = ImageSourceModule.fromResource("splashscreen.9");
|
||||
let imageSource = ImageSourceModule.fromResource("splashscreen");
|
||||
TKUnit.assertNotEqual(null, imageSource);
|
||||
image.imageSource = imageSource;
|
||||
helper.buildUIAndRunTest(image, () => {
|
||||
TKUnit.waitUntilReady(() => image.isLoaded);
|
||||
|
||||
Reference in New Issue
Block a user