mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-17 21:01:34 +08:00
Image-source modules refactoring
This commit is contained in:

committed by
Vladimir Enchev

parent
2e782aecc5
commit
ae8b285fc0
@ -135,6 +135,15 @@
|
||||
<TypeScriptCompile Include="apps\ui-tests-app\pages\i61.ts" />
|
||||
<TypeScriptCompile Include="apps\ui-tests-app\pages\i73.ts" />
|
||||
<TypeScriptCompile Include="apps\ui-tests-app\pages\gestures.ts" />
|
||||
<TypeScriptCompile Include="image-source\image-source-common.ts">
|
||||
<DependentUpon>image-source.d.ts</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="image-source\image-source.android.ts">
|
||||
<DependentUpon>image-source.d.ts</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="image-source\image-source.ios.ts">
|
||||
<DependentUpon>image-source.d.ts</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="platform\platform.ios.ts">
|
||||
<DependentUpon>platform.d.ts</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
@ -198,14 +207,7 @@
|
||||
<DependentUpon>http-request.d.ts</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="http\http.d.ts" />
|
||||
<TypeScriptCompile Include="image-source\image-source-native.android.ts">
|
||||
<DependentUpon>image-source-native.d.ts</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="image-source\image-source-native.ios.ts">
|
||||
<DependentUpon>image-source-native.d.ts</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="image-source\image-source.d.ts" />
|
||||
<TypeScriptCompile Include="image-source\image-source-native.d.ts" />
|
||||
<TypeScriptCompile Include="js-libs\easysax\easysax.d.ts" />
|
||||
<TypeScriptCompile Include="location\location.d.ts" />
|
||||
<TypeScriptCompile Include="apps\tests\app\pageNavigation.ts" />
|
||||
@ -651,9 +653,6 @@
|
||||
<DependentUpon>file-system.d.ts</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="file-system\file-system.d.ts" />
|
||||
<TypeScriptCompile Include="image-source\image-source.ts">
|
||||
<DependentUpon>image-source.d.ts</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
<TypeScriptCompile Include="location\location.ts">
|
||||
<DependentUpon>location.d.ts</DependentUpon>
|
||||
</TypeScriptCompile>
|
||||
@ -1467,7 +1466,7 @@
|
||||
<SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
|
||||
</WebProjectProperties>
|
||||
</FlavorProperties>
|
||||
<UserProperties ui_2layouts_2wrap-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2grid-layout_2package_1json__JSONSchema="" ui_2layouts_2dock-layout_2package_1json__JSONSchema="" ui_2layouts_2absolute-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2linear-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2web-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2content-view_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2gallery-app_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2absolute-layout-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2editable-text-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2scroll-view_2package_1json__JSONSchema="http://json.schemastore.org/package" />
|
||||
<UserProperties ui_2scroll-view_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2editable-text-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2absolute-layout-demo_2package_1json__JSONSchema="http://json.schemastore.org/package" apps_2gallery-app_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2content-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2web-view_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2linear-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2absolute-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" ui_2layouts_2dock-layout_2package_1json__JSONSchema="" ui_2layouts_2grid-layout_2package_1json__JSONSchema="" ui_2layouts_2wrap-layout_2package_1json__JSONSchema="http://json.schemastore.org/package" />
|
||||
</VisualStudio>
|
||||
</ProjectExtensions>
|
||||
</Project>
|
@ -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);
|
||||
// ```
|
||||
// </snippet>
|
||||
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,
|
||||
|
@ -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");
|
||||
}
|
||||
|
||||
|
37
image-source/image-source-common.ts
Normal file
37
image-source/image-source-common.ts
Normal file
@ -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<definition.ImageSource> {
|
||||
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);
|
||||
}
|
@ -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 = <android.graphics.drawable.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;
|
||||
}
|
||||
}
|
11
image-source/image-source-native.d.ts
vendored
11
image-source/image-source-native.d.ts
vendored
@ -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;
|
@ -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;
|
||||
}
|
112
image-source/image-source.android.ts
Normal file
112
image-source/image-source.android.ts
Normal file
@ -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 = <android.graphics.drawable.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;
|
||||
}
|
||||
}
|
25
image-source/image-source.d.ts
vendored
25
image-source/image-source.d.ts
vendored
@ -3,21 +3,6 @@
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
@ -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<ImageSource>;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
100
image-source/image-source.ios.ts
Normal file
100
image-source/image-source.ios.ts
Normal file
@ -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;
|
||||
}
|
@ -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<definition.ImageSource> {
|
||||
return http.getImage(url);
|
||||
}
|
16
ui/enums/enums.d.ts
vendored
16
ui/enums/enums.d.ts
vendored
@ -311,4 +311,20 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -85,3 +85,9 @@ export module AutocapitalizationType {
|
||||
export var sentences: string = "sentences";
|
||||
export var allCharacters: string = "allCharacters";
|
||||
}
|
||||
|
||||
|
||||
export module ImageFormat {
|
||||
export var png: string = "png";
|
||||
export var jpeg: string = "jpeg";
|
||||
}
|
||||
|
@ -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 = <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) {
|
||||
|
Reference in New Issue
Block a user