From 36e0f80a10efc55a0c8c8fc8fed5106130b5c493 Mon Sep 17 00:00:00 2001 From: vakrilov Date: Fri, 13 Mar 2015 15:03:18 +0200 Subject: [PATCH] Image-source modules refactoring --- CrossPlatformModules.csproj | 21 ++-- apps/tests/image-source-tests.ts | 7 +- apps/tests/ui/image/image-tests.ts | 1 - image-source/image-source-common.ts | 37 ++++++ image-source/image-source-native.android.ts | 68 ----------- image-source/image-source-native.d.ts | 11 -- image-source/image-source-native.ios.ts | 55 --------- image-source/image-source.android.ts | 112 +++++++++++++++++ image-source/image-source.d.ts | 27 ++-- image-source/image-source.ios.ts | 100 +++++++++++++++ image-source/image-source.ts | 129 -------------------- ui/enums/enums.d.ts | 30 +++-- ui/enums/enums.ts | 8 +- ui/image/image-common.ts | 33 ++--- 14 files changed, 309 insertions(+), 330 deletions(-) create mode 100644 image-source/image-source-common.ts delete mode 100644 image-source/image-source-native.android.ts delete mode 100644 image-source/image-source-native.d.ts delete mode 100644 image-source/image-source-native.ios.ts create mode 100644 image-source/image-source.android.ts create mode 100644 image-source/image-source.ios.ts delete mode 100644 image-source/image-source.ts diff --git a/CrossPlatformModules.csproj b/CrossPlatformModules.csproj index a2e24d433..17eab5445 100644 --- a/CrossPlatformModules.csproj +++ b/CrossPlatformModules.csproj @@ -135,6 +135,15 @@ + + image-source.d.ts + + + image-source.d.ts + + + image-source.d.ts + platform.d.ts @@ -198,14 +207,7 @@ http-request.d.ts - - image-source-native.d.ts - - - image-source-native.d.ts - - @@ -651,9 +653,6 @@ file-system.d.ts - - image-source.d.ts - location.d.ts @@ -1467,7 +1466,7 @@ False - + \ No newline at end of file diff --git a/apps/tests/image-source-tests.ts b/apps/tests/image-source-tests.ts index e819ec408..cf7cbfd4e 100644 --- a/apps/tests/image-source-tests.ts +++ b/apps/tests/image-source-tests.ts @@ -14,6 +14,7 @@ import imageSource = require("image-source"); import fs = require("file-system"); +import enums = require("ui/enums"); import app = require("application"); import TKUnit = require("./TKUnit"); import platform = require("platform"); @@ -73,7 +74,7 @@ export function testSaveToFile() { var img = imageSource.fromFile(imagePath); var folder = fs.knownFolders.documents(); var path = fs.path.join(folder.path, "Test.png"); - var saved = img.saveToFile(path, imageSource.ImageFormat.PNG); + var saved = img.saveToFile(path, enums.ImageFormat.png); // ``` // TKUnit.assert(saved, "Image not saved to file"); @@ -130,7 +131,7 @@ export function testBase64Encode_PNG() { } } - var result = img.toBase64String(imageSource.ImageFormat.PNG); + var result = img.toBase64String(enums.ImageFormat.png); TKUnit.assertEqual( result, expected, @@ -152,7 +153,7 @@ export function testBase64Encode_JPEG() { } } - var result = img.toBase64String(imageSource.ImageFormat.JPEG); + var result = img.toBase64String(enums.ImageFormat.jpeg); TKUnit.assertEqual( result, expected, diff --git a/apps/tests/ui/image/image-tests.ts b/apps/tests/ui/image/image-tests.ts index 9f2a88e40..3f491dcbd 100644 --- a/apps/tests/ui/image/image-tests.ts +++ b/apps/tests/ui/image/image-tests.ts @@ -36,7 +36,6 @@ var imagePath = __dirname + "../../logo.png"; export var test_Image_Members = function () { var image = new ImageModule.Image(); TKUnit.assert(types.isDefined(image.src), "Image.src is not defined"); - TKUnit.assert(types.isDefined(image.imageSource), "Image.imageSource is not defined"); TKUnit.assert(types.isDefined(image.isLoading), "Image.isLoading is not defined"); } diff --git a/image-source/image-source-common.ts b/image-source/image-source-common.ts new file mode 100644 index 000000000..2bbff7b3c --- /dev/null +++ b/image-source/image-source-common.ts @@ -0,0 +1,37 @@ +import http = require("http"); + +// This is used for definition purposes only, it does not generate JavaScript for it. +import definition = require("image-source"); + +var RESOURCE_PREFIX = "res://"; + +export function fromResource(name: string): definition.ImageSource { + var image = new definition.ImageSource(); + return image.loadFromResource(name) ? image : null; +} + +export function fromFile(path: string): definition.ImageSource { + var image = new definition.ImageSource(); + return image.loadFromFile(path) ? image : null; +} + +export function fromData(data: any): definition.ImageSource { + var image = new definition.ImageSource(); + return image.loadFromData(data) ? image : null; +} + +export function fromNativeSource(source: any): definition.ImageSource { + var image = new definition.ImageSource(); + return image.setNativeSource(source) ? image : null; +} + +export function fromUrl(url: string): Promise { + return http.getImage(url); +} + +export function fromFileOrResource(path: string): definition.ImageSource { + if (path.indexOf(RESOURCE_PREFIX) === 0) { + return fromResource(path.substr(RESOURCE_PREFIX.length)); + } + return fromFile(path); +} \ No newline at end of file diff --git a/image-source/image-source-native.android.ts b/image-source/image-source-native.android.ts deleted file mode 100644 index 53919ed2a..000000000 --- a/image-source/image-source-native.android.ts +++ /dev/null @@ -1,68 +0,0 @@ -import appModule = require("application"); - -export function fromResource(name: string) { - var androidApp = appModule.android; - var res = androidApp.context.getResources(); - if (res) { - var identifier: number = res.getIdentifier(name, 'drawable', androidApp.packageName); - if (0 < identifier) { - // Load BitmapDrawable with getDrawable to make use of Android internal caching - var bitmapDrawable = res.getDrawable(identifier); - if (bitmapDrawable && bitmapDrawable.getBitmap) { - return bitmapDrawable.getBitmap(); - } - } - } - - return null; -} - -export function fromFile(path: string) { - return android.graphics.BitmapFactory.decodeFile(path, null); -} - -export function fromData(data: any) { - return android.graphics.BitmapFactory.decodeStream(data); -} - -export function saveToFile(instance: android.graphics.Bitmap, path: string, format: number, quality = 100): boolean { - if (!instance) { - return false; - } - - var targetFormat = getTargetFromat(format); - - // TODO add exception handling - var outputStream = new java.io.BufferedOutputStream(new java.io.FileOutputStream(path)); - - var res = instance.compress(targetFormat, quality, outputStream); - outputStream.close(); - return res; -} - -export function toBase64String(instance: android.graphics.Bitmap, format: number, quality = 100): string { - if (!instance) { - return null;; - } - - var targetFormat = getTargetFromat(format); - - var outputStream = new java.io.ByteArrayOutputStream(); - var base64Stream = new android.util.Base64OutputStream(outputStream, android.util.Base64.NO_WRAP); - - instance.compress(targetFormat, quality, base64Stream); - - base64Stream.close(); - outputStream.close(); - - return outputStream.toString(); -} - -function getTargetFromat(format: number): android.graphics.Bitmap.CompressFormat { - switch (format) { - case 1: // JPEG - return android.graphics.Bitmap.CompressFormat.JPEG; - default: - return android.graphics.Bitmap.CompressFormat.PNG; - } -} \ No newline at end of file diff --git a/image-source/image-source-native.d.ts b/image-source/image-source-native.d.ts deleted file mode 100644 index 1b52383fa..000000000 --- a/image-source/image-source-native.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -//@private -/** - * This module is used as a native implementation for each of the underlying platforms. - * Users will not typically require it as it supports the module infrastructure. - */ -// -export declare function fromResource(name: string): any; -export declare function fromFile(path: string): any; -export declare function fromData(data: any): any; -export declare function saveToFile(instance: any, path: string, format: number, quality?: number): boolean; -export declare function toBase64String(instance: any, format: number, quality?: number): string; \ No newline at end of file diff --git a/image-source/image-source-native.ios.ts b/image-source/image-source-native.ios.ts deleted file mode 100644 index c88d9c095..000000000 --- a/image-source/image-source-native.ios.ts +++ /dev/null @@ -1,55 +0,0 @@ -export var fromResource = function (name: string) { - return UIImage.imageNamed(name); -} - -export var fromFile = function (path: string) { - return UIImage.imageWithContentsOfFile(path); -} - -export var fromData = function (data: any) { - return UIImage.imageWithData(data); -} - -export var saveToFile = function (instance: UIImage, path: string, format: number, quality?: number): boolean { - var res = false; - if (!instance) { - return res; - } - - var data = getImageData(instance, format, quality); - - if (data) { - res = data.writeToFileAtomically(path, true); - } - - return res; -} - -export function toBase64String(instance: UIImage, format: number, quality?: number): string { - var res = null; - if (!instance) { - return res; - } - - var data = getImageData(instance, format, quality); - - if (data) { - res = data.base64Encoding(); - } - - return res; -} - -function getImageData(instance: UIImage, format: number, quality: number): NSData { - var data = null; - switch (format) { - case 0: // PNG - data = UIImagePNGRepresentation(instance); - break; - case 1: // JPEG - data = UIImageJPEGRepresentation(instance, ('undefined' === typeof quality) ? 1.0 : quality); - break; - - } - return data; -} diff --git a/image-source/image-source.android.ts b/image-source/image-source.android.ts new file mode 100644 index 000000000..5c85f73f2 --- /dev/null +++ b/image-source/image-source.android.ts @@ -0,0 +1,112 @@ +import types = require("utils/types"); +import fs = require("file-system"); +import appModule = require("application"); +import definition = require("image-source"); +import common = require("image-source/image-source-common"); +import enums = require("ui/enums"); + +// merge the exports of the common file with the exports of this file +declare var exports; +require("utils/module-merge").merge(common, exports); + +export class ImageSource implements definition.ImageSource { + public android: android.graphics.Bitmap; + public ios: UIImage; + + public loadFromResource(name: string): boolean { + this.android = null; + + var androidApp = appModule.android; + var res = androidApp.context.getResources(); + if (res) { + var identifier: number = res.getIdentifier(name, 'drawable', androidApp.packageName); + if (0 < identifier) { + // Load BitmapDrawable with getDrawable to make use of Android internal caching + var bitmapDrawable = res.getDrawable(identifier); + if (bitmapDrawable && bitmapDrawable.getBitmap) { + this.android = bitmapDrawable.getBitmap(); + } + } + } + + return this.android != null; + } + + public loadFromFile(path: string): boolean { + var fileName = types.isString(path) ? path.trim() : ""; + if (fileName.indexOf("~/") === 0) { + fileName = fs.path.join(fs.knownFolders.currentApp().path, fileName.replace("~/", "")); + } + + this.android = android.graphics.BitmapFactory.decodeFile(fileName, null); + return this.android != null; + } + + public loadFromData(data: any): boolean { + this.android = android.graphics.BitmapFactory.decodeStream(data); + return this.android != null; + } + + public setNativeSource(source: any): boolean { + this.android = source; + return source != null; + } + + public saveToFile(path: string, format: string, quality = 100): boolean { + if (!this.android) { + return false; + } + + var targetFormat = getTargetFromat(format); + + // TODO add exception handling + var outputStream = new java.io.BufferedOutputStream(new java.io.FileOutputStream(path)); + + var res = this.android.compress(targetFormat, quality, outputStream); + outputStream.close(); + return res; + } + + public toBase64String(format: string, quality = 100): string { + if (!this.android) { + return null;; + } + + var targetFormat = getTargetFromat(format); + + var outputStream = new java.io.ByteArrayOutputStream(); + var base64Stream = new android.util.Base64OutputStream(outputStream, android.util.Base64.NO_WRAP); + + this.android.compress(targetFormat, quality, base64Stream); + + base64Stream.close(); + outputStream.close(); + + return outputStream.toString(); + } + + get height(): number { + if (this.android) { + return this.android.getHeight(); + } + + return NaN; + } + + get width(): number { + if (this.android) { + return this.android.getWidth(); + } + + return NaN; + } +} + +function getTargetFromat(format: string): android.graphics.Bitmap.CompressFormat { + switch (format) { + case enums.ImageFormat.jpeg: + return android.graphics.Bitmap.CompressFormat.JPEG; + default: + return android.graphics.Bitmap.CompressFormat.PNG; + } +} \ No newline at end of file diff --git a/image-source/image-source.d.ts b/image-source/image-source.d.ts index b4deaba7b..9ae5df5f5 100644 --- a/image-source/image-source.d.ts +++ b/image-source/image-source.d.ts @@ -3,22 +3,7 @@ */ declare module "image-source" { - /** - * Defines the recognized image formats. - */ - export enum ImageFormat { - /** - * The W3C Portable Network Graphics (PNG) image format. - */ - PNG, - - /** - * The Joint Photographic Experts Group (JPEG) image format. - */ - JPEG, - } - - /** + /** * Encapsulates the common abstraction behind a platform specific object (typically a Bitmap) that is used as a source for images. */ export class ImageSource { @@ -73,14 +58,14 @@ declare module "image-source" { * @param format The format (encoding) of the image. * @param quality Optional parameter, specifying the quality of the encoding. Defaults to the maximum available quality. */ - saveToFile(path: string, format: ImageFormat, quality?: number): boolean; + saveToFile(path: string, format: string, quality?: number): boolean; /** * Converts the image to base64 encoded string, using the provided image format and quality. * @param format The format (encoding) of the image. * @param quality Optional parameter, specifying the quality of the encoding. Defaults to the maximum available quality. */ - toBase64String(format: ImageFormat, quality?: number): string; + toBase64String(format: string, quality?: number): string; } /** @@ -113,4 +98,10 @@ declare module "image-source" { * @param url The link to the remote image object. This operation will download and decode the image. */ export function fromUrl(url: string): Promise; + + /** + * Creates a new ImageSource instance and loads it from the specified local file or resource(if spexified with "res://" prefix) + * @param path The location of the file on the file system. + */ + export function fromFileOrResource(path: string): ImageSource; } \ No newline at end of file diff --git a/image-source/image-source.ios.ts b/image-source/image-source.ios.ts new file mode 100644 index 000000000..3b191f6e8 --- /dev/null +++ b/image-source/image-source.ios.ts @@ -0,0 +1,100 @@ +import definition = require("image-source"); +import types = require("utils/types"); +import fs = require("file-system"); +import common = require("image-source/image-source-common"); +import enums = require("ui/enums"); + +// merge the exports of the common file with the exports of this file +declare var exports; +require("utils/module-merge").merge(common, exports); + +export class ImageSource implements definition.ImageSource { + public android: android.graphics.Bitmap; + public ios: UIImage; + + public loadFromResource(name: string): boolean { + this.ios = UIImage.imageNamed(name); + return this.ios != null; + } + + public loadFromFile(path: string): boolean { + var fileName = types.isString(path) ? path.trim() : ""; + + if (fileName.indexOf("~/") === 0) { + fileName = fs.path.join(fs.knownFolders.currentApp().path, fileName.replace("~/", "")); + } + + this.ios = UIImage.imageWithContentsOfFile(fileName); + return this.ios != null; + } + + public loadFromData(data: any): boolean { + this.ios = UIImage.imageWithData(data); + return this.ios != null; + } + + public setNativeSource(source: any): boolean { + this.ios = source; + return source != null; + } + + public saveToFile(path: string, format: string, quality?: number): boolean { + if (!this.ios) { + return false; + } + + var data = getImageData(this.ios, format, quality); + + if (data) { + return data.writeToFileAtomically(path, true); + } + + return false; + } + + public toBase64String(format: string, quality?: number): string { + var res = null; + if (!this.ios) { + return res; + } + + var data = getImageData(this.ios, format, quality); + + if (data) { + res = data.base64Encoding(); + } + + return res; + + } + + get height(): number { + if (this.ios) { + return this.ios.size.height; + } + + return NaN; + } + + get width(): number { + if (this.ios) { + return this.ios.size.width; + } + + return NaN; + } +} + +function getImageData(instance: UIImage, format: string, quality = 1.0): NSData { + var data = null; + switch (format) { + case enums.ImageFormat.png: // PNG + data = UIImagePNGRepresentation(instance); + break; + case enums.ImageFormat.jpeg: // JPEG + data = UIImageJPEGRepresentation(instance, quality); + break; + + } + return data; +} diff --git a/image-source/image-source.ts b/image-source/image-source.ts deleted file mode 100644 index 75a250408..000000000 --- a/image-source/image-source.ts +++ /dev/null @@ -1,129 +0,0 @@ -import native = require("image-source/image-source-native"); -import platform = require("platform"); -import types = require("utils/types"); -import fs = require("file-system"); -import http = require("http"); - -// This is used for definition purposes only, it does not generate JavaScript for it. -import definition = require("image-source"); - -export enum ImageFormat { - PNG, - JPEG, -} - -// TODO: Refactor into two files (.android.ts & .ios.ts); -export class ImageSource { - public android: android.graphics.Bitmap; - public ios: UIImage; - - constructor() { - this.setNativeInstance(null); - } - - public loadFromResource(name: string): boolean { - var nativeInstance = native.fromResource(name); - this.setNativeInstance(nativeInstance); - return nativeInstance != null; - } - - public loadFromFile(path: string): boolean { - var fileName = types.isString(path) ? path.trim() : ""; - - if (fileName.indexOf("~/") === 0) { - fileName = fs.path.join(fs.knownFolders.currentApp().path, fileName.replace("~/", "")); - } - - var nativeInstance = native.fromFile(fileName); - this.setNativeInstance(nativeInstance); - return (nativeInstance != null); - } - - public loadFromData(data: any): boolean { - var nativeInstance = native.fromData(data); - this.setNativeInstance(nativeInstance); - return (nativeInstance != null); - } - - public setNativeSource(source: any): boolean { - this.setNativeInstance(source); - return source != null; - } - - public saveToFile(path: string, format: ImageFormat, quality?: number): boolean { - return native.saveToFile(this.getNativeInstance(), path, format, quality); - } - - public toBase64String(format: ImageFormat, quality?: number): string { - return native.toBase64String(this.getNativeInstance(), format, quality); - } - - get height(): number { - // TODO: Refactor this, use class inheritance to overcome these switches - if (this.android) { - return this.android.getHeight(); - } - if (this.ios) { - return this.ios.size.height; - } - - return NaN; - } - - get width(): number { - // TODO: Refactor this, use class inheritance to overcome these switches - if (this.android) { - return this.android.getWidth(); - } - if (this.ios) { - return this.ios.size.width; - } - - return NaN; - } - - private setNativeInstance(instance: any) { - // TODO: Refactor this, use class inheritance to overcome these switches - if (platform.device.os === platform.platformNames.android) { - this.android = instance; - } else if (platform.device.os === platform.platformNames.ios) { - this.ios = instance; - } - } - - private getNativeInstance(): any { - // TODO: Refactor this, use class inheritance to overcome these switches - if (this.android) { - return this.android; - } - if (this.ios) { - return this.ios; - } - - return undefined; - } -} - -export function fromResource(name: string): ImageSource { - var image = new ImageSource(); - return image.loadFromResource(name) ? image : null; -} - -export function fromFile(path: string): ImageSource { - var image = new ImageSource(); - return image.loadFromFile(path) ? image : null; -} - -export function fromData(data: any): ImageSource { - var image = new ImageSource(); - return image.loadFromData(data) ? image : null; -} - -export function fromNativeSource(source: any): ImageSource { - var image = new ImageSource(); - return image.setNativeSource(source) ? image : null; -} - -export function fromUrl(url: string): Promise { - return http.getImage(url); -} \ No newline at end of file diff --git a/ui/enums/enums.d.ts b/ui/enums/enums.d.ts index 835945852..32a5b864a 100644 --- a/ui/enums/enums.d.ts +++ b/ui/enums/enums.d.ts @@ -9,25 +9,25 @@ * iOS: [UIKeyboardTypeNumbersAndPunctuation](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType) */ export var datetime: string; - + /** * Android: [TYPE_CLASS_PHONE](http://developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_PHONE) * iOS: [UIKeyboardTypePhonePad](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType) */ export var phone: string; - + /** * Android: [TYPE_CLASS_NUMBER](http://developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_NUMBER) | android.text.InputType.TYPE_NUMBER_VARIATION_NORMAL | [TYPE_NUMBER_FLAG_SIGNED](http://developer.android.com/reference/android/text/InputType.html#TYPE_NUMBER_FLAG_SIGNED) | [TYPE_NUMBER_FLAG_DECIMAL](http://developer.android.com/reference/android/text/InputType.html#TYPE_NUMBER_FLAG_DECIMAL) * iOS: [UIKeyboardTypeNumbersAndPunctuation](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType) */ export var number: string; - + /** * Android: [TYPE_CLASS_TEXT](http://developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_TEXT) | [TYPE_TEXT_VARIATION_URI](http://developer.android.com/reference/android/text/InputType.html#TYPE_TEXT_VARIATION_URI) * iOS: [UIKeyboardTypeURL](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType) */ export var url: string; - + /** * Android: [TYPE_CLASS_TEXT](http://developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_TEXT) | [TYPE_TEXT_VARIATION_EMAIL_ADDRESS](http://developer.android.com/reference/android/text/InputType.html#TYPE_TEXT_VARIATION_EMAIL_ADDRESS) * iOS: [UIKeyboardTypeEmailAddress](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIKeyboardType) @@ -56,13 +56,13 @@ * iOS: [UIReturnKeyGo](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIReturnKeyType) */ export var go: string; - + /** * Android: [IME_ACTION_SEARCH](http://developer.android.com/reference/android/view/inputmethod/EditorInfo.html#IME_ACTION_SEARCH) * iOS: [UIReturnKeySearch](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIReturnKeyType) */ export var search: string; - + /** * Android: [IME_ACTION_SEND](http://developer.android.com/reference/android/view/inputmethod/EditorInfo.html#IME_ACTION_SEND) * iOS: [UIReturnKeySend](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/c/tdef/UIReturnKeyType) @@ -304,11 +304,27 @@ * Capitalize the first letter of each sentence automatically. */ export var sentences: string; - + /** * Capitalize all characters automatically. */ export var allCharacters: string; } + + + /** + * Defines the recognized image formats. + */ + module ImageFormat { + /** + * The W3C Portable Network Graphics (PNG) image format. + */ + export var png: string; + + /** + * The Joint Photographic Experts Group (JPEG) image format. + */ + export var jpeg: string; + } } diff --git a/ui/enums/enums.ts b/ui/enums/enums.ts index 2b8e62bd9..e16a6082e 100644 --- a/ui/enums/enums.ts +++ b/ui/enums/enums.ts @@ -84,4 +84,10 @@ export module AutocapitalizationType { export var words: string = "words"; export var sentences: string = "sentences"; export var allCharacters: string = "allCharacters"; -} \ No newline at end of file +} + + +export module ImageFormat { + export var png: string = "png"; + export var jpeg: string = "jpeg"; +} diff --git a/ui/image/image-common.ts b/ui/image/image-common.ts index 6478fab01..e14b0fb50 100644 --- a/ui/image/image-common.ts +++ b/ui/image/image-common.ts @@ -15,54 +15,35 @@ var IMAGE = "Image"; var ISLOADING = "isLoading"; var STRETCH = "stretch"; -var RESOURCE_PREFIX = "res://"; - -function isResource(value: string): boolean { - return value.indexOf(RESOURCE_PREFIX) === 0; -} - function isUrl(value: string): boolean { return value.indexOf("http://") === 0 || value.indexOf("https://") === 0; } -function isAppFile(value: string): boolean { - return value.indexOf("~/") === 0; -} - -function isValidUrl(url: any): boolean { - if (!types.isString(url)) { - return false; - } - - var value = url ? url.trim() : ""; - return value !== "" && (isResource(value) || isAppFile(value) || isUrl(value)); +function isValidSrc(src: any): boolean { + return types.isString(src); } function onSrcPropertyChanged(data: dependencyObservable.PropertyChangeData) { var image = data.object; var value = data.newValue; - if (isValidUrl(value)) { + if (isValidSrc(value)) { value = value.trim(); image.imageSource = null; image["_url"] = value; image._setValue(Image.isLoadingProperty, true); - if (isResource(value)) { - image.imageSource = imageSource.fromResource(value.substr(RESOURCE_PREFIX.length)); - image._setValue(Image.isLoadingProperty, false); - } - else if (isAppFile(value)) { - image.imageSource = imageSource.fromFile(value); - image._setValue(Image.isLoadingProperty, false); - } else { + if (isUrl(value)) { imageSource.fromUrl(value).then((r) => { if (image["_url"] === value) { image.imageSource = r; image._setValue(Image.isLoadingProperty, false); } }); + } else { + image.imageSource = imageSource.fromFileOrResource(value); + image._setValue(Image.isLoadingProperty, false); } } else if (value instanceof imageSource.ImageSource) {