mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 03:31:45 +08:00
feat(image-asset-ios): add autoScaleFactor option to switch auto scaling (#6127)
This commit is contained in:

committed by
GitHub

parent
7e89f942b4
commit
81e63ee19e
@ -111,20 +111,37 @@ export function testFromAssetSimple(done) {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export function testFromAssetWithScaling(done) {
|
export function testFromAssetWithExactScaling(done) {
|
||||||
let asset = new imageAssetModule.ImageAsset(splashscreenPath);
|
let asset = new imageAssetModule.ImageAsset(splashscreenPath);
|
||||||
let scaleWidth = 10;
|
let scaleWidth = 10;
|
||||||
let scaleHeight = 11;
|
let scaleHeight = 11;
|
||||||
asset.options = {
|
asset.options = {
|
||||||
width: scaleWidth,
|
width: scaleWidth,
|
||||||
height: scaleHeight,
|
height: scaleHeight,
|
||||||
keepAspectRatio: false
|
keepAspectRatio: false,
|
||||||
|
autoScaleFactor: false
|
||||||
};
|
};
|
||||||
|
|
||||||
let img = imageSource.fromAsset(asset).then((source) => {
|
imageSource.fromAsset(asset).then((source) => {
|
||||||
TKUnit.assertEqual(source.width, scaleWidth);
|
TKUnit.assertEqual(source.width, scaleWidth);
|
||||||
TKUnit.assertEqual(source.height, scaleHeight);
|
TKUnit.assertEqual(source.height, scaleHeight);
|
||||||
done();
|
|
||||||
|
const targetFilename = `splashscreenTemp.png`;
|
||||||
|
const tempPath = fs.knownFolders.temp().path;
|
||||||
|
const localFullPath = fs.path.join(tempPath, targetFilename);
|
||||||
|
|
||||||
|
const fullImageSaved = source.saveToFile(localFullPath, "png");
|
||||||
|
|
||||||
|
if (fullImageSaved) {
|
||||||
|
let sourceImage = new imageSource.ImageSource();
|
||||||
|
sourceImage.fromFile(localFullPath).then(() => {
|
||||||
|
TKUnit.assertEqual(sourceImage.width, scaleWidth);
|
||||||
|
TKUnit.assertEqual(sourceImage.height, scaleHeight);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
done(`Error saving photo to local temp folder: ${localFullPath}`);
|
||||||
|
}
|
||||||
}, (error) => {
|
}, (error) => {
|
||||||
done(error);
|
done(error);
|
||||||
});
|
});
|
||||||
|
@ -2,16 +2,16 @@ import * as definition from ".";
|
|||||||
import * as observable from "../data/observable";
|
import * as observable from "../data/observable";
|
||||||
import * as platform from "../platform";
|
import * as platform from "../platform";
|
||||||
|
|
||||||
export class ImageAsset extends observable.Observable implements definition.ImageAsset {
|
export class ImageAsset extends observable.Observable implements definition.ImageAsset {
|
||||||
private _options: definition.ImageAssetOptions;
|
private _options: definition.ImageAssetOptions;
|
||||||
private _nativeImage: any;
|
private _nativeImage: any;
|
||||||
|
|
||||||
ios: PHAsset;
|
ios: PHAsset;
|
||||||
android: string;
|
android: string;
|
||||||
|
|
||||||
constructor () {
|
constructor() {
|
||||||
super();
|
super();
|
||||||
this._options = { keepAspectRatio: true };
|
this._options = { keepAspectRatio: true, autoScaleFactor: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
get options(): definition.ImageAssetOptions {
|
get options(): definition.ImageAssetOptions {
|
||||||
|
@ -17,4 +17,5 @@ export interface ImageAssetOptions {
|
|||||||
width?: number;
|
width?: number;
|
||||||
height?: number;
|
height?: number;
|
||||||
keepAspectRatio?: boolean;
|
keepAspectRatio?: boolean;
|
||||||
|
autoScaleFactor?: boolean;
|
||||||
}
|
}
|
||||||
|
@ -64,8 +64,11 @@ export class ImageAsset extends common.ImageAsset {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private scaleImage(image: UIImage, requestedSize: {width: number, height: number}): UIImage {
|
private scaleImage(image: UIImage, requestedSize: { width: number, height: number }): UIImage {
|
||||||
UIGraphicsBeginImageContextWithOptions(requestedSize, false, 0.0);
|
// scaleFactor = 0 takes the scale factor of the devices's main screen.
|
||||||
|
const scaleFactor = this.options && this.options.autoScaleFactor === false ? 1.0 : 0.0;
|
||||||
|
|
||||||
|
UIGraphicsBeginImageContextWithOptions(requestedSize, false, scaleFactor);
|
||||||
image.drawInRect(CGRectMake(0, 0, requestedSize.width, requestedSize.height));
|
image.drawInRect(CGRectMake(0, 0, requestedSize.width, requestedSize.height));
|
||||||
let resultImage = UIGraphicsGetImageFromCurrentImageContext();
|
let resultImage = UIGraphicsGetImageFromCurrentImageContext();
|
||||||
UIGraphicsEndImageContext();
|
UIGraphicsEndImageContext();
|
||||||
|
@ -190,7 +190,7 @@ function getFileName(path: string): string {
|
|||||||
return fileName;
|
return fileName;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getImageData(instance: UIImage, format: "png" | "jpeg" | "jpg", quality = 1.0): NSData {
|
function getImageData(instance: UIImage, format: "png" | "jpeg" | "jpg", quality = 0.9): NSData {
|
||||||
var data = null;
|
var data = null;
|
||||||
switch (format) {
|
switch (format) {
|
||||||
case "png":
|
case "png":
|
||||||
|
Reference in New Issue
Block a user