Merge pull request #2111 from NativeScript/widgets-ios-async-image

Optional async mode for loading images from local files
This commit is contained in:
Panayot Cankov
2016-05-20 09:11:36 +03:00
10 changed files with 266 additions and 108 deletions

View File

@@ -98,27 +98,14 @@ export function request(options: http.HttpRequestOptions): Promise<http.HttpResp
},
toImage: () => {
ensureImageSource();
if (UIImage.imageWithData["async"]) {
return UIImage.imageWithData["async"](UIImage, [data])
.then(image => {
if (!image) {
throw new Error("Response content may not be converted to an Image");
}
var source = new imageSource.ImageSource();
source.setNativeSource(image);
return source;
});
}
return new Promise<any>((resolveImage, rejectImage) => {
var img = imageSource.fromData(data);
if (img instanceof imageSource.ImageSource) {
resolveImage(img);
} else {
rejectImage(new Error("Response content may not be converted to an Image"));
}
return new Promise((resolve, reject) => {
(<any>UIImage).tns_decodeImageWithDataCompletion(data, image => {
if (image) {
resolve(imageSource.fromNativeSource(image))
} else {
reject(new Error("Response content may not be converted to an Image"));
}
});
});
},
toFile: (destinationFilePath?: string) => {

View File

@@ -33,12 +33,9 @@ export function getJSON<T>(arg: any): Promise<T> {
}
export function getImage(arg: any): Promise<image.ImageSource> {
return new Promise<image.ImageSource>((resolve, reject) => {
httpRequest.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
.then(r => {
r.content.toImage().then(source => resolve(source), e => reject(e));
}, e => reject(e));
});
return httpRequest
.request(typeof arg === "string" ? { url: arg, method: "GET" } : arg)
.then(responce => responce.content.toImage());
}
export function getFile(arg: any, destinationFilePath?: string): Promise<any> {