diff --git a/Image/image.android.ts b/Image/image.android.ts index cc6b88a3c..458e6ffbb 100644 --- a/Image/image.android.ts +++ b/Image/image.android.ts @@ -1,37 +1,17 @@ import appModule = require("Application/application"); -export enum ImageType { - PNG = 0, - JPEG = 1, +export enum ImageFormat { + PNG, + JPEG, } export class Image { - public android: any; + public android: android.graphics.Bitmap; constructor() { this.android = null; } - public static imageFromResource(name: string): Image { - var image = new Image(); - return image.loadFromResource(name) ? image : null; - } - - public static imageFromFile(path: string): Image { - var image = new Image(); - return image.loadFromFile(path) ? image : null; - } - - public static imageFromData(data: any): Image { - var image = new Image(); - return image.loadFromData(data) ? image : null; - } - - public static imageFromNativeBitmap(source: any): Image { - var image = new Image(); - return image.loadFromNativeBitmap(source) ? image : null; - } - public loadFromResource(name: string): boolean { var androidApp = appModule.current.android; var res = androidApp.context.getResources(); @@ -55,16 +35,16 @@ export class Image { return (this.android != null); } - public loadFromNativeBitmap(source: any): boolean { + public setNativeBitmap(source: any): boolean { this.android = source; return (this.android != null); } - public saveToFile(path: string, format: ImageType, quality?: number): boolean { + public saveToFile(path: string, format: ImageFormat, quality?: number): boolean { if (this.android) { var targetFormat = android.graphics.Bitmap.CompressFormat.PNG; switch (format) { - case ImageType.JPEG: + case ImageFormat.JPEG: targetFormat = android.graphics.Bitmap.CompressFormat.JPEG; break; } @@ -72,7 +52,7 @@ export class Image { // TODO add exception handling var outputStream = new java.io.BufferedOutputStream(new java.io.FileOutputStream(path)); - if (!quality) { + if (typeof quality == "undefined") { quality = 100; } @@ -83,11 +63,32 @@ export class Image { return false; } - public getHeight(): number { + get height(): number { return (this.android) ? this.android.getHeight() : NaN; } - public getWidth(): number { + get width(): number { return (this.android) ? this.android.getWidth() : NaN; } +} + +// TODO: These functions are the same in each platform, think for some common code separation +export function fromResource(name: string): Image { + var image = new Image(); + return image.loadFromResource(name) ? image : null; +} + +export function fromFile(path: string): Image { + var image = new Image(); + return image.loadFromFile(path) ? image : null; +} + +export function fromData(data: any): Image { + var image = new Image(); + return image.loadFromData(data) ? image : null; +} + +export function fromNativeBitmap(source: any): Image { + var image = new Image(); + return image.setNativeBitmap(source) ? image : null; } \ No newline at end of file diff --git a/Image/image.d.ts b/Image/image.d.ts index 0513c5453..b0edcbb0c 100644 --- a/Image/image.d.ts +++ b/Image/image.d.ts @@ -1,20 +1,79 @@ -export declare enum ImageType { - PNG = 0, - JPEG = 1, +/** +* Defines the recognized image formats. +*/ +export declare enum ImageFormat { + PNG, + JPEG, } +/** +* Encapsulates the common abstraction behind a platform specific image object. +*/ export declare class Image { - static imageFromResource(name: string): Image; - static imageFromFile(path: string): Image; - static imageFromData(data: any): Image; - static imageFromNativeBitmap(source: any): Image; + /** + * Gets the height of this instance. This is a read-only property. + */ + height: number; + /** + * Gets the width of this instance. This is a read-only property. + */ + width: number; + + /** + * The iOS-specific image instance. Will be undefined when running on Android. + */ + ios: UIKit.UIImage; + + /** + * The Android-specific image instance. Will be undefined when running on iOS. + */ + android: android.graphics.Bitmap; + + /** + * Loads this instance from the specified resource name. + */ loadFromResource(name: string): boolean; - loadFromFile(path: string): boolean; - loadFromData(data: any): boolean; - loadFromNativeBitmap(source: any): boolean; - saveToFile(path: string, format: ImageType, quality?: number): boolean; - getHeight(): number; - getWidth(): number; -} \ No newline at end of file + /** + * Loads this instance from the specified file. + */ + loadFromFile(path: string): boolean; + + /** + * Loads this instance from the specified native image data. + */ + loadFromData(data: any): boolean; + + /** + * Sets the provided native bitmap object. + * This will update either the android or ios properties, depending on the target os. + */ + setNativeBitmap(source: any): boolean; + + /** + * Saves this instance to the specified file, using the provided image format and quality. + */ + saveToFile(path: string, format: ImageFormat, quality?: number): boolean; +} + +/** +* Creates a new Image instance and loads it from the specified resource name. +*/ +export declare function fromResource(name: string): Image; + +/** +* Creates a new Image instance and loads it from the specified file. +*/ +export declare function fromFile(path: string): Image; + +/** +* Creates a new Image instance and loads it from the specified resource name. +*/ +export declare function fromData(data: any): Image; + +/** +* Creates a new Image instance and sets the provided native bitmap object. +* The native bitmap object will update either the android or ios properties, depending on the target os. +*/ +export declare function fromNativeBitmap(source: any): Image; \ No newline at end of file diff --git a/Image/image.ios.ts b/Image/image.ios.ts index c0d762254..2df1493cc 100644 --- a/Image/image.ios.ts +++ b/Image/image.ios.ts @@ -1,35 +1,15 @@ -export enum ImageType { - PNG = 0, - JPEG = 1, +export enum ImageFormat { + PNG, + JPEG, } export class Image { - public ios: any; + public ios: UIKit.UIImage; constructor() { this.ios = null; } - public static imageFromResource(name: string): Image { - var image = new Image(); - return image.loadFromResource(name) ? image : null; - } - - public static imageFromFile(path: string): Image { - var image = new Image(); - return image.loadFromFile(path) ? image : null; - } - - public static imageFromData(data: any): Image { - var image = new Image(); - return image.loadFromData(data) ? image : null; - } - - public static imageFromNativeBitmap(source: any): Image { - var image = new Image(); - return image.loadFromNativeBitmap(source) ? image : null; - } - public loadFromResource(name: string): boolean { this.ios = UIKit.UIImage.imageNamed(name); return (this.ios != null); @@ -45,22 +25,22 @@ export class Image { return (this.ios != null); } - public loadFromNativeBitmap(source: any): boolean { + public setNativeBitmap(source: any): boolean { this.ios = source; return (this.ios != null); } - public saveToFile(path: string, format: ImageType, quality?: number): boolean { + public saveToFile(path: string, format: ImageFormat, quality?: number): boolean { if (null == this.ios) { return false; } var res = false; var data = null; switch (format) { - case ImageType.JPEG: + case ImageFormat.JPEG: data = UIKit.UIImageJPEGRepresentation(this.ios, ('undefined' == typeof quality) ? 1.0 : quality); break; - case ImageType.PNG: + case ImageFormat.PNG: data = UIKit.UIImagePNGRepresentation(this.ios); break; } @@ -70,11 +50,32 @@ export class Image { return res; } - public getHeight(): number { + get height(): number { return (this.ios) ? this.ios.size.height : NaN; } - public getWidth(): number { + get width(): number { return (this.ios) ? this.ios.size.width : NaN; } +} + +// TODO: These functions are the same in each platform, think for some common code separation +export function fromResource(name: string): Image { + var image = new Image(); + return image.loadFromResource(name) ? image : null; +} + +export function fromFile(path: string): Image { + var image = new Image(); + return image.loadFromFile(path) ? image : null; +} + +export function fromData(data: any): Image { + var image = new Image(); + return image.loadFromData(data) ? image : null; +} + +export function fromNativeBitmap(source: any): Image { + var image = new Image(); + return image.setNativeBitmap(source) ? image : null; } \ No newline at end of file