mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
Refactored the Image module. Added image.fromUrl method. Added image-tests.
This commit is contained in:
@@ -1,94 +0,0 @@
|
||||
import appModule = require("application/application");
|
||||
|
||||
export enum ImageFormat {
|
||||
PNG,
|
||||
JPEG,
|
||||
}
|
||||
|
||||
export class Image {
|
||||
public android: android.graphics.Bitmap;
|
||||
|
||||
constructor() {
|
||||
this.android = null;
|
||||
}
|
||||
|
||||
public loadFromResource(name: string): boolean {
|
||||
var androidApp = appModule.android;
|
||||
var res = androidApp.context.getResources();
|
||||
if (res) {
|
||||
var identifier: number = res.getIdentifier(name, 'drawable', androidApp.packageName);
|
||||
if (0 < identifier) {
|
||||
this.android = android.graphics.BitmapFactory.decodeResource(res, identifier);
|
||||
return (this.android != null);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public loadFromFile(path: string): boolean {
|
||||
this.android = android.graphics.BitmapFactory.decodeFile(path, null);
|
||||
return (this.android != null);
|
||||
}
|
||||
|
||||
public loadFromData(data: any): boolean {
|
||||
this.android = android.graphics.BitmapFactory.decodeStream(data);
|
||||
return (this.android != null);
|
||||
}
|
||||
|
||||
public setNativeBitmap(source: any): boolean {
|
||||
this.android = source;
|
||||
return (this.android != null);
|
||||
}
|
||||
|
||||
public saveToFile(path: string, format: ImageFormat, quality?: number): boolean {
|
||||
if (this.android) {
|
||||
var targetFormat = android.graphics.Bitmap.CompressFormat.PNG;
|
||||
switch (format) {
|
||||
case ImageFormat.JPEG:
|
||||
targetFormat = android.graphics.Bitmap.CompressFormat.JPEG;
|
||||
break;
|
||||
}
|
||||
|
||||
// TODO add exception handling
|
||||
var outputStream = new java.io.BufferedOutputStream(new java.io.FileOutputStream(path));
|
||||
|
||||
if (typeof quality == "undefined") {
|
||||
quality = 100;
|
||||
}
|
||||
|
||||
var res = this.android.compress(targetFormat, quality, outputStream);
|
||||
outputStream.close();
|
||||
return res;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
get height(): number {
|
||||
return (this.android) ? this.android.getHeight() : NaN;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
5
image/image.d.ts
vendored
5
image/image.d.ts
vendored
@@ -80,4 +80,7 @@ export declare function fromData(data: any): Image;
|
||||
*/
|
||||
export declare function fromNativeBitmap(source: any): Image;
|
||||
|
||||
// export declare function fromUrl
|
||||
/**
|
||||
* Downloads the image from the provided Url and creates a new Image instance from it.
|
||||
*/
|
||||
export declare function fromUrl(url: string): promises.Promise<Image>;
|
||||
@@ -1,81 +0,0 @@
|
||||
export enum ImageFormat {
|
||||
PNG,
|
||||
JPEG,
|
||||
}
|
||||
|
||||
export class Image {
|
||||
public ios: UIKit.UIImage;
|
||||
|
||||
constructor() {
|
||||
this.ios = null;
|
||||
}
|
||||
|
||||
public loadFromResource(name: string): boolean {
|
||||
this.ios = UIKit.UIImage.imageNamed(name);
|
||||
return (this.ios != null);
|
||||
}
|
||||
|
||||
public loadFromFile(path: string): boolean {
|
||||
this.ios = UIKit.UIImage.imageWithContentsOfFile(path);
|
||||
return (this.ios != null);
|
||||
}
|
||||
|
||||
public loadFromData(data: any): boolean {
|
||||
this.ios = UIKit.UIImage.imageWithData(data);
|
||||
return (this.ios != null);
|
||||
}
|
||||
|
||||
public setNativeBitmap(source: any): boolean {
|
||||
this.ios = source;
|
||||
return (this.ios != null);
|
||||
}
|
||||
|
||||
public saveToFile(path: string, format: ImageFormat, quality?: number): boolean {
|
||||
if (null == this.ios) {
|
||||
return false;
|
||||
}
|
||||
var res = false;
|
||||
var data = null;
|
||||
switch (format) {
|
||||
case ImageFormat.JPEG:
|
||||
data = UIKit.UIImageJPEGRepresentation(this.ios, ('undefined' == typeof quality) ? 1.0 : quality);
|
||||
break;
|
||||
case ImageFormat.PNG:
|
||||
data = UIKit.UIImagePNGRepresentation(this.ios);
|
||||
break;
|
||||
}
|
||||
if (null != data) {
|
||||
res = data.writeToFileAtomically(path, true);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
get height(): number {
|
||||
return (this.ios) ? this.ios.size.height : NaN;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
110
image/image.ts
Normal file
110
image/image.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import app = require("application/application");
|
||||
import impl = require("image/image_impl");
|
||||
import promises = require("promises/promises");
|
||||
import http = require("http/http");
|
||||
|
||||
export enum ImageFormat {
|
||||
PNG,
|
||||
JPEG,
|
||||
}
|
||||
|
||||
export class Image {
|
||||
public android: android.graphics.Bitmap;
|
||||
public ios: UIKit.UIImage;
|
||||
|
||||
constructor() {
|
||||
this.setNativeInstance(null);
|
||||
}
|
||||
|
||||
public loadFromResource(name: string): boolean {
|
||||
var nativeInstance = impl.fromResource(name);
|
||||
this.setNativeInstance(nativeInstance);
|
||||
return nativeInstance != null;
|
||||
}
|
||||
|
||||
public loadFromFile(path: string): boolean {
|
||||
var nativeInstance = impl.fromFile(path);
|
||||
this.setNativeInstance(nativeInstance);
|
||||
return (nativeInstance != null);
|
||||
}
|
||||
|
||||
public loadFromData(data: any): boolean {
|
||||
var nativeInstance = impl.fromData(data);
|
||||
this.setNativeInstance(nativeInstance);
|
||||
return (nativeInstance != null);
|
||||
}
|
||||
|
||||
public setNativeBitmap(source: any): boolean {
|
||||
this.setNativeInstance(source);
|
||||
return source != null;
|
||||
}
|
||||
|
||||
public saveToFile(path: string, format: ImageFormat, quality?: number): boolean {
|
||||
return impl.saveToFile(this.getNativeInstance(), path, format, quality);
|
||||
}
|
||||
|
||||
get height(): number {
|
||||
if (this.android) {
|
||||
return this.android.getHeight();
|
||||
}
|
||||
if (this.ios) {
|
||||
return this.ios.size.height;
|
||||
}
|
||||
|
||||
return NaN;
|
||||
}
|
||||
|
||||
get width(): number {
|
||||
if (this.android) {
|
||||
return this.android.getWidth();
|
||||
}
|
||||
if (this.ios) {
|
||||
return this.ios.size.width;
|
||||
}
|
||||
|
||||
return NaN;
|
||||
}
|
||||
|
||||
private setNativeInstance(instance: any) {
|
||||
if (app.android) {
|
||||
this.android = instance;
|
||||
} else if (app.ios) {
|
||||
this.ios = instance;
|
||||
}
|
||||
}
|
||||
|
||||
private getNativeInstance(): any {
|
||||
if (this.android) {
|
||||
return this.android;
|
||||
}
|
||||
if (this.ios) {
|
||||
return this.ios;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
export function fromUrl(url: string): promises.Promise<Image> {
|
||||
return http.getImage(url);
|
||||
}
|
||||
42
image/image_impl.android.ts
Normal file
42
image/image_impl.android.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import appModule = require("application/application");
|
||||
|
||||
export var fromResource = function (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) {
|
||||
return android.graphics.BitmapFactory.decodeResource(res, identifier);
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
export var fromFile = function (path: string) {
|
||||
return android.graphics.BitmapFactory.decodeFile(path, null);
|
||||
}
|
||||
|
||||
export var fromData = function (data: any) {
|
||||
return android.graphics.BitmapFactory.decodeStream(data);
|
||||
}
|
||||
|
||||
export var saveToFile = function (instance: android.graphics.Bitmap, path: string, format: number, quality = 100): boolean {
|
||||
if (!instance) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var targetFormat = android.graphics.Bitmap.CompressFormat.PNG;
|
||||
switch (format) {
|
||||
case 1: // JPEG
|
||||
targetFormat = android.graphics.Bitmap.CompressFormat.JPEG;
|
||||
break;
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
8
image/image_impl.d.ts
vendored
Normal file
8
image/image_impl.d.ts
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* 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;
|
||||
33
image/image_impl.ios.ts
Normal file
33
image/image_impl.ios.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
export var fromResource = function (name: string) {
|
||||
return UIKit.UIImage.imageNamed(name);
|
||||
}
|
||||
|
||||
export var fromFile = function (path: string) {
|
||||
return UIKit.UIImage.imageWithContentsOfFile(path);
|
||||
}
|
||||
|
||||
export var fromData = function (data: any) {
|
||||
return UIKit.UIImage.imageWithData(data);
|
||||
}
|
||||
|
||||
export var saveToFile = function (instance: UIKit.UIImage, path: string, format: number, quality?: number): boolean {
|
||||
if (!instance) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var res = false;
|
||||
var data = null;
|
||||
switch (format) {
|
||||
case 0: // PNG
|
||||
data = UIKit.UIImagePNGRepresentation(instance);
|
||||
break;
|
||||
case 1: // JPEG
|
||||
data = UIKit.UIImageJPEGRepresentation(instance, ('undefined' == typeof quality) ? 1.0 : quality);
|
||||
break;
|
||||
|
||||
}
|
||||
if (null != data) {
|
||||
res = data.writeToFileAtomically(path, true);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
Reference in New Issue
Block a user