@ticnat/nativescript-image-cache
Version:
Plugin for caching images
62 lines • 2.59 kB
JavaScript
/// <reference path="./types.d.ts" />
import { Utils } from '@nativescript/core';
import { ImageSource } from '@nativescript/core/image-source';
import { isString } from '@nativescript/core/utils/types';
export class Helpers {
static onStretchPropertyChanged(nativeView, value) {
switch (value) {
case 'aspectFit':
nativeView.contentMode = UIViewContentMode.UIViewContentModeScaleAspectFit;
break;
case 'aspectFill':
nativeView.contentMode = UIViewContentMode.UIViewContentModeScaleAspectFill;
break;
case 'fill':
nativeView.contentMode = UIViewContentMode.UIViewContentModeScaleToFill;
break;
case 'none':
default:
nativeView.contentMode = UIViewContentMode.UIViewContentModeTopLeft;
break;
}
}
static onSrcPropertySet(nativeWrapper, value) {
let image = nativeWrapper, placeholder = nativeWrapper.placeholder, placeholderImage = this.getPlaceholderUIImage(placeholder);
if (isString(value)) {
value = value.trim();
if (0 === value.indexOf('http')) {
image.isLoading = true;
image.nativeView.sd_setImageWithURLPlaceholderImageCompleted(value, placeholderImage, function () {
image.isLoading = false;
});
}
else if (Utils.isFileOrResourcePath(value)) {
image.isLoading = true;
let source = new ImageSource();
if (0 === value.indexOf(Utils.RESOURCE_PREFIX)) {
let path = value.substr(Utils.RESOURCE_PREFIX.length);
ImageSource.fromResource(path).then(function () {
image.isLoading = false;
image.nativeView.image = source.ios || source.nativeView;
});
}
else {
ImageSource.fromFile(value).then(function () {
image.isLoading = false;
image.nativeView.image = source.ios || source.nativeView;
});
}
}
image.requestLayout();
}
}
static getPlaceholderUIImage(value) {
if (isString(value)) {
if (Utils.isFileOrResourcePath(value)) {
return ImageSource.fromFileOrResourceSync(value).ios;
}
}
return undefined;
}
}
//# sourceMappingURL=helpers.js.map