mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
chore: cleanup image handling
This commit is contained in:
@@ -4,6 +4,7 @@ import { ImageAsset } from '../image-asset';
|
||||
import * as httpModule from '../http';
|
||||
import { Font } from '../ui/styling/font';
|
||||
import { Color } from '../color';
|
||||
import { Trace } from '../trace';
|
||||
|
||||
// Types.
|
||||
import { path as fsPath, knownFolders } from '../file-system';
|
||||
@@ -84,8 +85,10 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
if (image) {
|
||||
resolve(new ImageSource(image));
|
||||
} else {
|
||||
(<any>UIImage).tns_safeDecodeImageNamedCompletion(`${name}.jpg`, (image) => {
|
||||
resolve(new ImageSource(image));
|
||||
(<any>UIImage).tns_safeDecodeImageNamedCompletion(`${name}.jpg`, (img) => {
|
||||
if (img) {
|
||||
resolve(new ImageSource(img));
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
@@ -104,7 +107,9 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
return new Promise<ImageSource>((resolve, reject) => {
|
||||
try {
|
||||
(<any>UIImage).tns_decodeImageWidthContentsOfFileCompletion(getFileName(path), (uiImage) => {
|
||||
resolve(new ImageSource(uiImage));
|
||||
if (uiImage) {
|
||||
resolve(new ImageSource(uiImage));
|
||||
}
|
||||
});
|
||||
} catch (ex) {
|
||||
reject(ex);
|
||||
@@ -114,7 +119,10 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
|
||||
static fromFileOrResourceSync(path: string): ImageSource {
|
||||
if (!isFileOrResourcePath(path)) {
|
||||
throw new Error('Path "' + '" is not a valid file or resource.');
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write('Path "' + path + '" is not a valid file or resource.', Trace.categories.Binding, Trace.messageType.error);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if (path.indexOf(RESOURCE_PREFIX) === 0) {
|
||||
@@ -133,7 +141,9 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
return new Promise<ImageSource>((resolve, reject) => {
|
||||
try {
|
||||
(<any>UIImage).tns_decodeImageWithDataCompletion(data, (uiImage) => {
|
||||
resolve(new ImageSource(uiImage));
|
||||
if (uiImage) {
|
||||
resolve(new ImageSource(uiImage));
|
||||
}
|
||||
});
|
||||
} catch (ex) {
|
||||
reject(ex);
|
||||
@@ -295,7 +305,10 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
|
||||
public setNativeSource(source: any): void {
|
||||
if (source && !(source instanceof UIImage)) {
|
||||
throw new Error('The method setNativeSource() expects UIImage instance.');
|
||||
if (Trace.isEnabled()) {
|
||||
Trace.write('The method setNativeSource() expects UIImage instance.', Trace.categories.Binding, Trace.messageType.error);
|
||||
}
|
||||
return;
|
||||
}
|
||||
this.ios = source;
|
||||
}
|
||||
|
||||
@@ -40,20 +40,24 @@ export class Image extends ImageBase {
|
||||
}
|
||||
|
||||
private setTintColor(value: Color) {
|
||||
if (value && this.nativeViewProtected.image && !this._templateImageWasCreated) {
|
||||
this.nativeViewProtected.image = this.nativeViewProtected.image.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
|
||||
this._templateImageWasCreated = true;
|
||||
} else if (!value && this.nativeViewProtected.image && this._templateImageWasCreated) {
|
||||
this._templateImageWasCreated = false;
|
||||
this.nativeViewProtected.image = this.nativeViewProtected.image.imageWithRenderingMode(UIImageRenderingMode.Automatic);
|
||||
if (this.nativeViewProtected) {
|
||||
if (value && this.nativeViewProtected.image && !this._templateImageWasCreated) {
|
||||
this.nativeViewProtected.image = this.nativeViewProtected.image.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
|
||||
this._templateImageWasCreated = true;
|
||||
} else if (!value && this.nativeViewProtected.image && this._templateImageWasCreated) {
|
||||
this._templateImageWasCreated = false;
|
||||
this.nativeViewProtected.image = this.nativeViewProtected.image.imageWithRenderingMode(UIImageRenderingMode.Automatic);
|
||||
}
|
||||
this.nativeViewProtected.tintColor = value ? value.ios : null;
|
||||
}
|
||||
this.nativeViewProtected.tintColor = value ? value.ios : null;
|
||||
}
|
||||
|
||||
public _setNativeImage(nativeImage: UIImage) {
|
||||
this.nativeViewProtected.image = nativeImage;
|
||||
if (this.nativeViewProtected) {
|
||||
this.nativeViewProtected.image = nativeImage;
|
||||
}
|
||||
this._templateImageWasCreated = false;
|
||||
this.setTintColor(this.style.tintColor);
|
||||
this.setTintColor(this.style?.tintColor);
|
||||
|
||||
if (this._imageSourceAffectsLayout) {
|
||||
this.requestLayout();
|
||||
@@ -61,8 +65,10 @@ export class Image extends ImageBase {
|
||||
}
|
||||
|
||||
_setNativeClipToBounds() {
|
||||
// Always set clipsToBounds for images
|
||||
this.nativeViewProtected.clipsToBounds = true;
|
||||
if (this.nativeViewProtected) {
|
||||
// Always set clipsToBounds for images
|
||||
this.nativeViewProtected.clipsToBounds = true;
|
||||
}
|
||||
}
|
||||
|
||||
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
|
||||
@@ -134,23 +140,25 @@ export class Image extends ImageBase {
|
||||
}
|
||||
|
||||
[stretchProperty.setNative](value: 'none' | 'aspectFill' | 'aspectFit' | 'fill') {
|
||||
switch (value) {
|
||||
case 'aspectFit':
|
||||
this.nativeViewProtected.contentMode = UIViewContentMode.ScaleAspectFit;
|
||||
break;
|
||||
if (this.nativeViewProtected) {
|
||||
switch (value) {
|
||||
case 'aspectFit':
|
||||
this.nativeViewProtected.contentMode = UIViewContentMode.ScaleAspectFit;
|
||||
break;
|
||||
|
||||
case 'aspectFill':
|
||||
this.nativeViewProtected.contentMode = UIViewContentMode.ScaleAspectFill;
|
||||
break;
|
||||
case 'aspectFill':
|
||||
this.nativeViewProtected.contentMode = UIViewContentMode.ScaleAspectFill;
|
||||
break;
|
||||
|
||||
case 'fill':
|
||||
this.nativeViewProtected.contentMode = UIViewContentMode.ScaleToFill;
|
||||
break;
|
||||
case 'fill':
|
||||
this.nativeViewProtected.contentMode = UIViewContentMode.ScaleToFill;
|
||||
break;
|
||||
|
||||
case 'none':
|
||||
default:
|
||||
this.nativeViewProtected.contentMode = UIViewContentMode.TopLeft;
|
||||
break;
|
||||
case 'none':
|
||||
default:
|
||||
this.nativeViewProtected.contentMode = UIViewContentMode.TopLeft;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user