mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
async image loading from data, files and resources for ios
Use the tns_safeDecodeImageNamedCompletion from the widgets framework Add loadMode on Image with sync and async options for local images
This commit is contained in:
committed by
Panayot Cankov
parent
b8785afd74
commit
1b395aac7f
@@ -44,7 +44,7 @@ export class ImageSource implements definition.ImageSource {
|
||||
// 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();
|
||||
this.android = bitmapDrawable.getBitmap();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,12 @@ export class ImageSource implements definition.ImageSource {
|
||||
return this.android != null;
|
||||
}
|
||||
|
||||
public fromResource(name: string): Promise<boolean> {
|
||||
return new Promise<boolean>((resolve, reject) => {
|
||||
resolve(this.loadFromResource(name));
|
||||
});
|
||||
}
|
||||
|
||||
public loadFromFile(path: string): boolean {
|
||||
ensureFS();
|
||||
|
||||
@@ -64,11 +70,23 @@ export class ImageSource implements definition.ImageSource {
|
||||
return this.android != null;
|
||||
}
|
||||
|
||||
public fromFile(path: string): Promise<boolean> {
|
||||
return new Promise<boolean>((resolve, reject) => {
|
||||
resolve(this.loadFromFile(path));
|
||||
});
|
||||
}
|
||||
|
||||
public loadFromData(data: any): boolean {
|
||||
this.android = android.graphics.BitmapFactory.decodeStream(data);
|
||||
return this.android != null;
|
||||
}
|
||||
|
||||
public fromData(data: any): Promise<boolean> {
|
||||
return new Promise<boolean>((resolve, reject) => {
|
||||
resolve(this.loadFromData(data));
|
||||
});
|
||||
}
|
||||
|
||||
public loadFromBase64(source: string): boolean {
|
||||
if (types.isString(source)) {
|
||||
var bytes = android.util.Base64.decode(source, android.util.Base64.DEFAULT);
|
||||
@@ -77,6 +95,12 @@ export class ImageSource implements definition.ImageSource {
|
||||
return this.android != null;
|
||||
}
|
||||
|
||||
public fromBase64(data: any): Promise<boolean> {
|
||||
return new Promise<boolean>((resolve, reject) => {
|
||||
resolve(this.loadFromBase64(data));
|
||||
});
|
||||
}
|
||||
|
||||
public setNativeSource(source: any): boolean {
|
||||
this.android = source;
|
||||
return source != null;
|
||||
|
||||
24
image-source/image-source.d.ts
vendored
24
image-source/image-source.d.ts
vendored
@@ -33,23 +33,47 @@ declare module "image-source" {
|
||||
*/
|
||||
loadFromResource(name: string): boolean;
|
||||
|
||||
/**
|
||||
* Loads this instance from the specified resource name asynchronously.
|
||||
* @param name The name of the resource (without its extension).
|
||||
*/
|
||||
fromResource(name: string): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* Loads this instance from the specified file.
|
||||
* @param path The location of the file on the file system.
|
||||
*/
|
||||
loadFromFile(path: string): boolean;
|
||||
|
||||
/**
|
||||
* Loads this instance from the specified file asynchronously.
|
||||
* @param path The location of the file on the file system.
|
||||
*/
|
||||
fromFile(path: string): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* Loads this instance from the specified native image data.
|
||||
* @param data The native data (byte array) to load the image from. This will be either Stream for Android or NSData for iOS.
|
||||
*/
|
||||
loadFromData(data: any): boolean;
|
||||
|
||||
/**
|
||||
* Loads this instance from the specified native image data asynchronously.
|
||||
* @param data The native data (byte array) to load the image from. This will be either Stream for Android or NSData for iOS.
|
||||
*/
|
||||
fromData(data: any): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* Loads this instance from the specified native image data.
|
||||
* @param source The Base64 string to load the image from.
|
||||
*/
|
||||
loadFromBase64(source: string): boolean;
|
||||
|
||||
/**
|
||||
* Loads this instance from the specified native image data asynchronously.
|
||||
* @param source The Base64 string to load the image from.
|
||||
*/
|
||||
fromBase64(source: string): Promise<boolean>;
|
||||
|
||||
/**
|
||||
* Sets the provided native source object (typically a Bitmap).
|
||||
|
||||
@@ -11,10 +11,30 @@ export class ImageSource implements definition.ImageSource {
|
||||
public ios: UIImage;
|
||||
|
||||
public loadFromResource(name: string): boolean {
|
||||
this.ios = UIImage.imageNamed(name) || UIImage.imageNamed(`${name}.jpg`);
|
||||
this.ios = (<any>UIImage).tns_safeImageNamed(name) || (<any>UIImage).tns_safeImageNamed(`${name}.jpg`);
|
||||
return this.ios != null;
|
||||
}
|
||||
|
||||
public fromResource(name: string): Promise<boolean> {
|
||||
return new Promise<boolean>((resolve, reject) => {
|
||||
try {
|
||||
(<any>UIImage).tns_safeDecodeImageNamedCompletion(name, image => {
|
||||
if (image) {
|
||||
this.ios = image;
|
||||
resolve(true);
|
||||
} else {
|
||||
(<any>UIImage).tns_safeDecodeImageNamedCompletion(`${name}.jpg`, image => {
|
||||
this.ios = image;
|
||||
resolve(true);
|
||||
});
|
||||
}
|
||||
});
|
||||
} catch (ex) {
|
||||
reject(ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public loadFromFile(path: string): boolean {
|
||||
var fileName = types.isString(path) ? path.trim() : "";
|
||||
|
||||
@@ -26,19 +46,67 @@ export class ImageSource implements definition.ImageSource {
|
||||
return this.ios != null;
|
||||
}
|
||||
|
||||
public fromFile(path: string): Promise<boolean> {
|
||||
return new Promise<boolean>((resolve, reject) => {
|
||||
try {
|
||||
var fileName = types.isString(path) ? path.trim() : "";
|
||||
|
||||
if (fileName.indexOf("~/") === 0) {
|
||||
fileName = fs.path.join(fs.knownFolders.currentApp().path, fileName.replace("~/", ""));
|
||||
}
|
||||
|
||||
(<any>UIImage).tns_decodeImageWidthContentsOfFileCompletion(fileName, image => {
|
||||
this.ios = image;
|
||||
resolve(true);
|
||||
});
|
||||
} catch (ex) {
|
||||
reject(ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public loadFromData(data: any): boolean {
|
||||
this.ios = UIImage.imageWithData(data);
|
||||
return this.ios != null;
|
||||
}
|
||||
|
||||
public fromData(data: any): Promise<boolean> {
|
||||
return new Promise<boolean>((resolve, reject) => {
|
||||
try {
|
||||
(<any>UIImage).tns_decodeImageWithDataCompletion(data, image => {
|
||||
this.ios = image;
|
||||
resolve(true);
|
||||
});
|
||||
} catch (ex) {
|
||||
reject(ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public loadFromBase64(source: string): boolean {
|
||||
if (types.isString(source)) {
|
||||
var data = NSData.alloc().initWithBase64EncodedStringOptions(source, NSDataBase64DecodingOptions.NSDataBase64DecodingIgnoreUnknownCharacters);
|
||||
this.ios = UIImage.imageWithData(data);
|
||||
}
|
||||
|
||||
return this.ios != null;
|
||||
}
|
||||
|
||||
public fromBase64(source: string): Promise<boolean> {
|
||||
return new Promise<boolean>((resolve, reject) => {
|
||||
try {
|
||||
var data = NSData.alloc().initWithBase64EncodedStringOptions(source, NSDataBase64DecodingOptions.NSDataBase64DecodingIgnoreUnknownCharacters);
|
||||
UIImage.imageWithData["async"](UIImage, [data]).then(image => {
|
||||
this.ios = image;
|
||||
resolve(true);
|
||||
});
|
||||
|
||||
} catch (ex) {
|
||||
reject(ex);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public setNativeSource(source: any): boolean {
|
||||
this.ios = source;
|
||||
return source != null;
|
||||
@@ -103,4 +171,4 @@ function getImageData(instance: UIImage, format: string, quality = 1.0): NSData
|
||||
|
||||
}
|
||||
return data;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user