mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Merge branch 'master'
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
import * as imageSource from "tns-core-modules/image-source";
|
||||
import * as fs from "tns-core-modules/file-system";
|
||||
import { ImageSource } from "@nativescript/core/image-source";
|
||||
import * as fs from "@nativescript/core/file-system";
|
||||
// >> imagesource-from-imageasset-save-to
|
||||
|
||||
export function imageSourceFromAsset(imageAsset) {
|
||||
let source = new imageSource.ImageSource();
|
||||
source.fromAsset(imageAsset).then((imageSource) => {
|
||||
ImageSource.fromAsset(imageAsset).then((imageSource) => {
|
||||
let folder = fs.knownFolders.documents().path;
|
||||
let fileName = "test.png";
|
||||
let path = fs.path.join(folder, fileName);
|
||||
@@ -12,6 +11,6 @@ export function imageSourceFromAsset(imageAsset) {
|
||||
if (saved) {
|
||||
console.log("Image saved successfully!");
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
// << imagesource-from-imageasset-save-to
|
||||
// << imagesource-from-imageasset-save-to
|
||||
|
||||
@@ -1,30 +1,31 @@
|
||||
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";
|
||||
import { ImageSource } from "@nativescript/core/image-source";
|
||||
import * as imageAssetModule from "@nativescript/core/image-asset";
|
||||
import * as fs from "@nativescript/core/file-system";
|
||||
import * as app from "@nativescript/core/application";
|
||||
import * as TKUnit from "../tk-unit";
|
||||
import { Font } from "@nativescript/core/ui/styling/font";
|
||||
import { Color } from "@nativescript/core/color";
|
||||
|
||||
const imagePath = "~/logo.png";
|
||||
const splashscreenPath = "~/splashscreen.png";
|
||||
const imagePath = "~/assets/logo.png";
|
||||
const splashscreenPath = "~/assets/splashscreen.png";
|
||||
const splashscreenWidth = 372;
|
||||
const splashscreenHeight = 218;
|
||||
const smallImagePath = "~/small-image.png";
|
||||
const smallImagePath = "~/assets/small-image.png";
|
||||
|
||||
export function testFromResource() {
|
||||
// >> imagesource-resname
|
||||
const img = imageSource.fromResource("icon");
|
||||
const img = ImageSource.fromResourceSync("icon");
|
||||
// << imagesource-resname
|
||||
|
||||
TKUnit.assert(img.height > 0, "image.fromResource failed");
|
||||
}
|
||||
|
||||
export function testFromUrl(done) {
|
||||
let result: imageSource.ImageSource;
|
||||
let result: ImageSource;
|
||||
|
||||
// Deprecated method fromUrl
|
||||
imageSource.fromUrl("https://www.google.com/images/errors/logo_sm_2.png")
|
||||
.then((res: imageSource.ImageSource) => {
|
||||
ImageSource.fromUrl("https://www.google.com/images/errors/logo_sm_2.png")
|
||||
.then((res: ImageSource) => {
|
||||
// console.log("Image successfully loaded");
|
||||
// completed = true;
|
||||
result = res;
|
||||
@@ -45,7 +46,7 @@ export function testFromUrl(done) {
|
||||
|
||||
export function testSaveToFile() {
|
||||
// >> imagesource-save-to
|
||||
const img = imageSource.fromFile(imagePath);
|
||||
const img = ImageSource.fromFileSync(imagePath);
|
||||
const folder = fs.knownFolders.documents();
|
||||
const path = fs.path.join(folder.path, "test.png");
|
||||
const saved = img.saveToFile(path, "png");
|
||||
@@ -55,7 +56,7 @@ export function testSaveToFile() {
|
||||
}
|
||||
|
||||
export function testSaveToFile_WithQuality() {
|
||||
const img = imageSource.fromFile(imagePath);
|
||||
const img = ImageSource.fromFileSync(imagePath);
|
||||
const folder = fs.knownFolders.documents();
|
||||
const path = fs.path.join(folder.path, "test.png");
|
||||
const saved = img.saveToFile(path, "png", 70);
|
||||
@@ -67,7 +68,7 @@ export function testFromFile() {
|
||||
// >> imagesource-load-local
|
||||
const folder = fs.knownFolders.documents();
|
||||
const path = fs.path.join(folder.path, "test.png");
|
||||
const img = imageSource.fromFile(path);
|
||||
const img = ImageSource.fromFileSync(path);
|
||||
// << imagesource-load-local
|
||||
|
||||
TKUnit.assert(img.height > 0, "image.fromResource failed");
|
||||
@@ -86,7 +87,7 @@ export function testFromAssetFileNotFound(done) {
|
||||
keepAspectRatio: true
|
||||
};
|
||||
|
||||
let img = imageSource.fromAsset(asset).then((source) => {
|
||||
ImageSource.fromAsset(asset).then((source) => {
|
||||
done("Should not resolve with invalid file name.");
|
||||
}, (error) => {
|
||||
TKUnit.assertNotNull(error);
|
||||
@@ -102,7 +103,7 @@ export function testFromAssetSimple(done) {
|
||||
keepAspectRatio: true
|
||||
};
|
||||
|
||||
let img = imageSource.fromAsset(asset).then((source) => {
|
||||
ImageSource.fromAsset(asset).then((source) => {
|
||||
TKUnit.assertEqual(source.width, splashscreenWidth);
|
||||
TKUnit.assertEqual(source.height, splashscreenHeight);
|
||||
done();
|
||||
@@ -122,7 +123,7 @@ export function testFromAssetWithExactScaling(done) {
|
||||
autoScaleFactor: false
|
||||
};
|
||||
|
||||
imageSource.fromAsset(asset).then((source) => {
|
||||
ImageSource.fromAsset(asset).then((source) => {
|
||||
TKUnit.assertEqual(source.width, scaleWidth);
|
||||
TKUnit.assertEqual(source.height, scaleHeight);
|
||||
|
||||
@@ -133,8 +134,7 @@ export function testFromAssetWithExactScaling(done) {
|
||||
const fullImageSaved = source.saveToFile(localFullPath, "png");
|
||||
|
||||
if (fullImageSaved) {
|
||||
let sourceImage = new imageSource.ImageSource();
|
||||
sourceImage.fromFile(localFullPath).then(() => {
|
||||
ImageSource.fromFile(localFullPath).then((sourceImage) => {
|
||||
TKUnit.assertEqual(sourceImage.width, scaleWidth);
|
||||
TKUnit.assertEqual(sourceImage.height, scaleHeight);
|
||||
done();
|
||||
@@ -157,7 +157,7 @@ export function testFromAssetWithScalingAndAspectRatio(done) {
|
||||
keepAspectRatio: true
|
||||
};
|
||||
|
||||
let img = imageSource.fromAsset(asset).then((source) => {
|
||||
ImageSource.fromAsset(asset).then((source) => {
|
||||
TKUnit.assertEqual(source.width, 18);
|
||||
TKUnit.assertEqual(source.height, scaleHeight);
|
||||
done();
|
||||
@@ -173,7 +173,7 @@ export function testFromAssetWithScalingAndDefaultAspectRatio(done) {
|
||||
asset.options.width = scaleWidth;
|
||||
asset.options.height = scaleHeight;
|
||||
|
||||
let img = imageSource.fromAsset(asset).then((source) => {
|
||||
ImageSource.fromAsset(asset).then((source) => {
|
||||
TKUnit.assertEqual(source.width, 18);
|
||||
TKUnit.assertEqual(source.height, scaleHeight);
|
||||
done();
|
||||
@@ -192,7 +192,7 @@ export function testFromAssetWithBiggerScaling(done) {
|
||||
keepAspectRatio: false
|
||||
};
|
||||
|
||||
let img = imageSource.fromAsset(asset).then((source) => {
|
||||
ImageSource.fromAsset(asset).then((source) => {
|
||||
TKUnit.assertEqual(source.width, scaleWidth);
|
||||
TKUnit.assertEqual(source.height, scaleHeight);
|
||||
done();
|
||||
@@ -202,7 +202,7 @@ export function testFromAssetWithBiggerScaling(done) {
|
||||
}
|
||||
|
||||
export function testNativeFields() {
|
||||
const img = imageSource.fromFile(imagePath);
|
||||
const img = ImageSource.fromFileSync(imagePath);
|
||||
if (app.android) {
|
||||
TKUnit.assert(img.android != null, "Image.android not updated.");
|
||||
} else if (app.ios) {
|
||||
@@ -218,7 +218,7 @@ const expectedPngStart = "iVBORw0KGgoAAAANSUhEUgAAAAQAAAAECAIAAAAmkwkpAAA";
|
||||
|
||||
export function testBase64Encode_PNG() {
|
||||
// >> imagesource-to-base-string
|
||||
const img = imageSource.fromFile(smallImagePath);
|
||||
const img = ImageSource.fromFileSync(smallImagePath);
|
||||
let base64String = img.toBase64String("png");
|
||||
// << imagesource-to-base-string
|
||||
|
||||
@@ -230,7 +230,7 @@ export function testBase64Encode_PNG() {
|
||||
}
|
||||
|
||||
export function testBase64Encode_PNG_WithQuality() {
|
||||
const img = imageSource.fromFile(smallImagePath);
|
||||
const img = ImageSource.fromFileSync(smallImagePath);
|
||||
let base64String = img.toBase64String("png", 80);
|
||||
base64String = base64String.substr(0, expectedPngStart.length);
|
||||
TKUnit.assertEqual(
|
||||
@@ -240,7 +240,7 @@ export function testBase64Encode_PNG_WithQuality() {
|
||||
}
|
||||
|
||||
export function testBase64Encode_JPEG() {
|
||||
const img = imageSource.fromFile(smallImagePath);
|
||||
const img = ImageSource.fromFileSync(smallImagePath);
|
||||
|
||||
let base64String = img.toBase64String("jpeg");
|
||||
base64String = base64String.substr(0, expectedJpegStart.length);
|
||||
@@ -252,7 +252,7 @@ export function testBase64Encode_JPEG() {
|
||||
}
|
||||
|
||||
export function testBase64Encode_JPEG_With_Quality() {
|
||||
const img = imageSource.fromFile(smallImagePath);
|
||||
const img = ImageSource.fromFileSync(smallImagePath);
|
||||
|
||||
let base64String = img.toBase64String("jpeg", 80);
|
||||
base64String = base64String.substr(0, expectedJpegStart.length);
|
||||
@@ -265,8 +265,8 @@ export function testBase64Encode_JPEG_With_Quality() {
|
||||
|
||||
export function testLoadFromBase64Encode_JPEG() {
|
||||
// >> imagesource-from-base-string
|
||||
let img: imageSource.ImageSource;
|
||||
img = imageSource.fromBase64(jpgImageAsBase64String);
|
||||
let img: ImageSource;
|
||||
img = ImageSource.fromBase64Sync(jpgImageAsBase64String);
|
||||
// << imagesource-from-base-string
|
||||
|
||||
TKUnit.assert(img !== null, "Actual: " + img);
|
||||
@@ -275,14 +275,23 @@ export function testLoadFromBase64Encode_JPEG() {
|
||||
}
|
||||
|
||||
export function testLoadFromBase64Encode_PNG() {
|
||||
let img: imageSource.ImageSource;
|
||||
let img: ImageSource;
|
||||
if (app.android) {
|
||||
img = imageSource.fromBase64(fullAndroidPng);
|
||||
img = ImageSource.fromBase64Sync(fullAndroidPng);
|
||||
} else if (app.ios) {
|
||||
img = imageSource.fromBase64(fullIosPng);
|
||||
img = ImageSource.fromBase64Sync(fullIosPng);
|
||||
}
|
||||
|
||||
TKUnit.assert(img !== null, "Actual: " + img);
|
||||
TKUnit.assertEqual(img.width, 4, "img.width");
|
||||
TKUnit.assertEqual(img.height, 4, "img.height");
|
||||
}
|
||||
|
||||
export function testLoadFromFontIconCode() {
|
||||
let img: ImageSource;
|
||||
img = ImageSource.fromFontIconCodeSync("F10B", Font.default.withFontFamily("FontAwesome"), new Color("red"));
|
||||
|
||||
TKUnit.assert(img !== null, "Actual: " + img);
|
||||
TKUnit.assert(img.width !== null, "img.width");
|
||||
TKUnit.assert(img.height !== null, "img.width");
|
||||
}
|
||||
|
||||
@@ -53,4 +53,4 @@ The supported formats are `png`, `jpeg`, and `jpg`. The quality parameter is opt
|
||||
|
||||
### Creating PNG image file from base64 string
|
||||
The method `fromBase64(source: string): Promise<boolean>` loads this instance from the specified base64 encoded string asynchronously.
|
||||
{%snippet imagesource-from-base-string%}
|
||||
{%snippet imagesource-from-base-string%}
|
||||
|
||||
Reference in New Issue
Block a user