Polished the Image module. Added API reference.

This commit is contained in:
atanasovg
2014-05-09 17:40:08 +03:00
parent a4ddb134fd
commit 7aa70fb14c
3 changed files with 135 additions and 74 deletions

View File

@ -1,37 +1,17 @@
import appModule = require("Application/application"); import appModule = require("Application/application");
export enum ImageType { export enum ImageFormat {
PNG = 0, PNG,
JPEG = 1, JPEG,
} }
export class Image { export class Image {
public android: any; public android: android.graphics.Bitmap;
constructor() { constructor() {
this.android = null; 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 { public loadFromResource(name: string): boolean {
var androidApp = appModule.current.android; var androidApp = appModule.current.android;
var res = androidApp.context.getResources(); var res = androidApp.context.getResources();
@ -55,16 +35,16 @@ export class Image {
return (this.android != null); return (this.android != null);
} }
public loadFromNativeBitmap(source: any): boolean { public setNativeBitmap(source: any): boolean {
this.android = source; this.android = source;
return (this.android != null); 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) { if (this.android) {
var targetFormat = android.graphics.Bitmap.CompressFormat.PNG; var targetFormat = android.graphics.Bitmap.CompressFormat.PNG;
switch (format) { switch (format) {
case ImageType.JPEG: case ImageFormat.JPEG:
targetFormat = android.graphics.Bitmap.CompressFormat.JPEG; targetFormat = android.graphics.Bitmap.CompressFormat.JPEG;
break; break;
} }
@ -72,7 +52,7 @@ export class Image {
// TODO add exception handling // TODO add exception handling
var outputStream = new java.io.BufferedOutputStream(new java.io.FileOutputStream(path)); var outputStream = new java.io.BufferedOutputStream(new java.io.FileOutputStream(path));
if (!quality) { if (typeof quality == "undefined") {
quality = 100; quality = 100;
} }
@ -83,11 +63,32 @@ export class Image {
return false; return false;
} }
public getHeight(): number { get height(): number {
return (this.android) ? this.android.getHeight() : NaN; return (this.android) ? this.android.getHeight() : NaN;
} }
public getWidth(): number { get width(): number {
return (this.android) ? this.android.getWidth() : NaN; 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;
} }

87
Image/image.d.ts vendored
View File

@ -1,20 +1,79 @@
export declare enum ImageType { /**
PNG = 0, * Defines the recognized image formats.
JPEG = 1, */
export declare enum ImageFormat {
PNG,
JPEG,
} }
/**
* Encapsulates the common abstraction behind a platform specific image object.
*/
export declare class Image { export declare class Image {
static imageFromResource(name: string): Image; /**
static imageFromFile(path: string): Image; * Gets the height of this instance. This is a read-only property.
static imageFromData(data: any): Image; */
static imageFromNativeBitmap(source: any): Image; 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; 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; * 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;

View File

@ -1,35 +1,15 @@
export enum ImageType { export enum ImageFormat {
PNG = 0, PNG,
JPEG = 1, JPEG,
} }
export class Image { export class Image {
public ios: any; public ios: UIKit.UIImage;
constructor() { constructor() {
this.ios = null; 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 { public loadFromResource(name: string): boolean {
this.ios = UIKit.UIImage.imageNamed(name); this.ios = UIKit.UIImage.imageNamed(name);
return (this.ios != null); return (this.ios != null);
@ -45,22 +25,22 @@ export class Image {
return (this.ios != null); return (this.ios != null);
} }
public loadFromNativeBitmap(source: any): boolean { public setNativeBitmap(source: any): boolean {
this.ios = source; this.ios = source;
return (this.ios != null); 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) { if (null == this.ios) {
return false; return false;
} }
var res = false; var res = false;
var data = null; var data = null;
switch (format) { switch (format) {
case ImageType.JPEG: case ImageFormat.JPEG:
data = UIKit.UIImageJPEGRepresentation(this.ios, ('undefined' == typeof quality) ? 1.0 : quality); data = UIKit.UIImageJPEGRepresentation(this.ios, ('undefined' == typeof quality) ? 1.0 : quality);
break; break;
case ImageType.PNG: case ImageFormat.PNG:
data = UIKit.UIImagePNGRepresentation(this.ios); data = UIKit.UIImagePNGRepresentation(this.ios);
break; break;
} }
@ -70,11 +50,32 @@ export class Image {
return res; return res;
} }
public getHeight(): number { get height(): number {
return (this.ios) ? this.ios.size.height : NaN; return (this.ios) ? this.ios.size.height : NaN;
} }
public getWidth(): number { get width(): number {
return (this.ios) ? this.ios.size.width : NaN; 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;
} }