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 * as httpModule from '../http';
|
||||||
import { Font } from '../ui/styling/font';
|
import { Font } from '../ui/styling/font';
|
||||||
import { Color } from '../color';
|
import { Color } from '../color';
|
||||||
|
import { Trace } from '../trace';
|
||||||
|
|
||||||
// Types.
|
// Types.
|
||||||
import { path as fsPath, knownFolders } from '../file-system';
|
import { path as fsPath, knownFolders } from '../file-system';
|
||||||
@@ -84,8 +85,10 @@ export class ImageSource implements ImageSourceDefinition {
|
|||||||
if (image) {
|
if (image) {
|
||||||
resolve(new ImageSource(image));
|
resolve(new ImageSource(image));
|
||||||
} else {
|
} else {
|
||||||
(<any>UIImage).tns_safeDecodeImageNamedCompletion(`${name}.jpg`, (image) => {
|
(<any>UIImage).tns_safeDecodeImageNamedCompletion(`${name}.jpg`, (img) => {
|
||||||
resolve(new ImageSource(image));
|
if (img) {
|
||||||
|
resolve(new ImageSource(img));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -104,7 +107,9 @@ export class ImageSource implements ImageSourceDefinition {
|
|||||||
return new Promise<ImageSource>((resolve, reject) => {
|
return new Promise<ImageSource>((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
(<any>UIImage).tns_decodeImageWidthContentsOfFileCompletion(getFileName(path), (uiImage) => {
|
(<any>UIImage).tns_decodeImageWidthContentsOfFileCompletion(getFileName(path), (uiImage) => {
|
||||||
|
if (uiImage) {
|
||||||
resolve(new ImageSource(uiImage));
|
resolve(new ImageSource(uiImage));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
reject(ex);
|
reject(ex);
|
||||||
@@ -114,7 +119,10 @@ export class ImageSource implements ImageSourceDefinition {
|
|||||||
|
|
||||||
static fromFileOrResourceSync(path: string): ImageSource {
|
static fromFileOrResourceSync(path: string): ImageSource {
|
||||||
if (!isFileOrResourcePath(path)) {
|
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) {
|
if (path.indexOf(RESOURCE_PREFIX) === 0) {
|
||||||
@@ -133,7 +141,9 @@ export class ImageSource implements ImageSourceDefinition {
|
|||||||
return new Promise<ImageSource>((resolve, reject) => {
|
return new Promise<ImageSource>((resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
(<any>UIImage).tns_decodeImageWithDataCompletion(data, (uiImage) => {
|
(<any>UIImage).tns_decodeImageWithDataCompletion(data, (uiImage) => {
|
||||||
|
if (uiImage) {
|
||||||
resolve(new ImageSource(uiImage));
|
resolve(new ImageSource(uiImage));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
reject(ex);
|
reject(ex);
|
||||||
@@ -295,7 +305,10 @@ export class ImageSource implements ImageSourceDefinition {
|
|||||||
|
|
||||||
public setNativeSource(source: any): void {
|
public setNativeSource(source: any): void {
|
||||||
if (source && !(source instanceof UIImage)) {
|
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;
|
this.ios = source;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ export class Image extends ImageBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private setTintColor(value: Color) {
|
private setTintColor(value: Color) {
|
||||||
|
if (this.nativeViewProtected) {
|
||||||
if (value && this.nativeViewProtected.image && !this._templateImageWasCreated) {
|
if (value && this.nativeViewProtected.image && !this._templateImageWasCreated) {
|
||||||
this.nativeViewProtected.image = this.nativeViewProtected.image.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
|
this.nativeViewProtected.image = this.nativeViewProtected.image.imageWithRenderingMode(UIImageRenderingMode.AlwaysTemplate);
|
||||||
this._templateImageWasCreated = true;
|
this._templateImageWasCreated = true;
|
||||||
@@ -49,11 +50,14 @@ export class Image extends ImageBase {
|
|||||||
}
|
}
|
||||||
this.nativeViewProtected.tintColor = value ? value.ios : null;
|
this.nativeViewProtected.tintColor = value ? value.ios : null;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public _setNativeImage(nativeImage: UIImage) {
|
public _setNativeImage(nativeImage: UIImage) {
|
||||||
|
if (this.nativeViewProtected) {
|
||||||
this.nativeViewProtected.image = nativeImage;
|
this.nativeViewProtected.image = nativeImage;
|
||||||
|
}
|
||||||
this._templateImageWasCreated = false;
|
this._templateImageWasCreated = false;
|
||||||
this.setTintColor(this.style.tintColor);
|
this.setTintColor(this.style?.tintColor);
|
||||||
|
|
||||||
if (this._imageSourceAffectsLayout) {
|
if (this._imageSourceAffectsLayout) {
|
||||||
this.requestLayout();
|
this.requestLayout();
|
||||||
@@ -61,9 +65,11 @@ export class Image extends ImageBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_setNativeClipToBounds() {
|
_setNativeClipToBounds() {
|
||||||
|
if (this.nativeViewProtected) {
|
||||||
// Always set clipsToBounds for images
|
// Always set clipsToBounds for images
|
||||||
this.nativeViewProtected.clipsToBounds = true;
|
this.nativeViewProtected.clipsToBounds = true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
|
public onMeasure(widthMeasureSpec: number, heightMeasureSpec: number): void {
|
||||||
// We don't call super because we measure native view with specific size.
|
// We don't call super because we measure native view with specific size.
|
||||||
@@ -134,6 +140,7 @@ export class Image extends ImageBase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
[stretchProperty.setNative](value: 'none' | 'aspectFill' | 'aspectFit' | 'fill') {
|
[stretchProperty.setNative](value: 'none' | 'aspectFill' | 'aspectFit' | 'fill') {
|
||||||
|
if (this.nativeViewProtected) {
|
||||||
switch (value) {
|
switch (value) {
|
||||||
case 'aspectFit':
|
case 'aspectFit':
|
||||||
this.nativeViewProtected.contentMode = UIViewContentMode.ScaleAspectFit;
|
this.nativeViewProtected.contentMode = UIViewContentMode.ScaleAspectFit;
|
||||||
@@ -153,6 +160,7 @@ export class Image extends ImageBase {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
[tintColorProperty.setNative](value: Color) {
|
[tintColorProperty.setNative](value: Color) {
|
||||||
this.setTintColor(value);
|
this.setTintColor(value);
|
||||||
|
|||||||
Reference in New Issue
Block a user