mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
renamed folders with lower cases and renamed user preferences to local settings
This commit is contained in:
6
image/Readme.md
Normal file
6
image/Readme.md
Normal file
@@ -0,0 +1,6 @@
|
||||
The way we get local path here is different for now. Maybe we should get it from FS module in the future samples. Sample code Android:
|
||||
|
||||
```
|
||||
var Image = require("Image").Image;
|
||||
var baseImage = Image.imageFromResource('foxie');
|
||||
```
|
||||
94
image/image.android.ts
Normal file
94
image/image.android.ts
Normal file
@@ -0,0 +1,94 @@
|
||||
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;
|
||||
}
|
||||
79
image/image.d.ts
vendored
Normal file
79
image/image.d.ts
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* 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 {
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
81
image/image.ios.ts
Normal file
81
image/image.ios.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
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;
|
||||
}
|
||||
2
image/index.ts
Normal file
2
image/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
declare var module, require;
|
||||
module.exports = require("image/image");
|
||||
Reference in New Issue
Block a user