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
@@ -1,13 +1,21 @@
|
||||
import * as common from "./image-asset-common";
|
||||
import { path as fsPath, knownFolders } from "../file-system";
|
||||
|
||||
global.moduleMerge(common, exports);
|
||||
|
||||
export class ImageAsset extends common.ImageAsset {
|
||||
private _ios: PHAsset;
|
||||
|
||||
constructor(asset: PHAsset | UIImage) {
|
||||
constructor(asset: string | PHAsset | UIImage) {
|
||||
super();
|
||||
if (asset instanceof UIImage) {
|
||||
if (typeof asset === "string") {
|
||||
if (asset.indexOf("~/") === 0) {
|
||||
asset = fsPath.join(knownFolders.currentApp().path, asset.replace("~/", ""));
|
||||
}
|
||||
|
||||
this.nativeImage = UIImage.imageWithContentsOfFile(asset);
|
||||
}
|
||||
else if (asset instanceof UIImage) {
|
||||
this.nativeImage = asset
|
||||
}
|
||||
else {
|
||||
@@ -24,6 +32,10 @@ export class ImageAsset extends common.ImageAsset {
|
||||
}
|
||||
|
||||
public getImageAsync(callback: (image, error) => void) {
|
||||
if (!this.ios && !this.nativeImage) {
|
||||
callback(null, "Asset cannot be found.");
|
||||
}
|
||||
|
||||
let srcWidth = this.nativeImage ? this.nativeImage.size.width : this.ios.pixelWidth;
|
||||
let srcHeight = this.nativeImage ? this.nativeImage.size.height : this.ios.pixelHeight;
|
||||
let requestedSize = common.getRequestedImageSize({ width: srcWidth, height: srcHeight }, this.options);
|
||||
|
||||
Reference in New Issue
Block a user