mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-16 03:31:45 +08:00
Image-source modules refactoring
This commit is contained in:
100
image-source/image-source.ios.ts
Normal file
100
image-source/image-source.ios.ts
Normal file
@ -0,0 +1,100 @@
|
||||
import definition = require("image-source");
|
||||
import types = require("utils/types");
|
||||
import fs = require("file-system");
|
||||
import common = require("image-source/image-source-common");
|
||||
import enums = require("ui/enums");
|
||||
|
||||
// merge the exports of the common file with the exports of this file
|
||||
declare var exports;
|
||||
require("utils/module-merge").merge(common, exports);
|
||||
|
||||
export class ImageSource implements definition.ImageSource {
|
||||
public android: android.graphics.Bitmap;
|
||||
public ios: UIImage;
|
||||
|
||||
public loadFromResource(name: string): boolean {
|
||||
this.ios = UIImage.imageNamed(name);
|
||||
return this.ios != null;
|
||||
}
|
||||
|
||||
public loadFromFile(path: string): boolean {
|
||||
var fileName = types.isString(path) ? path.trim() : "";
|
||||
|
||||
if (fileName.indexOf("~/") === 0) {
|
||||
fileName = fs.path.join(fs.knownFolders.currentApp().path, fileName.replace("~/", ""));
|
||||
}
|
||||
|
||||
this.ios = UIImage.imageWithContentsOfFile(fileName);
|
||||
return this.ios != null;
|
||||
}
|
||||
|
||||
public loadFromData(data: any): boolean {
|
||||
this.ios = UIImage.imageWithData(data);
|
||||
return this.ios != null;
|
||||
}
|
||||
|
||||
public setNativeSource(source: any): boolean {
|
||||
this.ios = source;
|
||||
return source != null;
|
||||
}
|
||||
|
||||
public saveToFile(path: string, format: string, quality?: number): boolean {
|
||||
if (!this.ios) {
|
||||
return false;
|
||||
}
|
||||
|
||||
var data = getImageData(this.ios, format, quality);
|
||||
|
||||
if (data) {
|
||||
return data.writeToFileAtomically(path, true);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public toBase64String(format: string, quality?: number): string {
|
||||
var res = null;
|
||||
if (!this.ios) {
|
||||
return res;
|
||||
}
|
||||
|
||||
var data = getImageData(this.ios, format, quality);
|
||||
|
||||
if (data) {
|
||||
res = data.base64Encoding();
|
||||
}
|
||||
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
get height(): number {
|
||||
if (this.ios) {
|
||||
return this.ios.size.height;
|
||||
}
|
||||
|
||||
return NaN;
|
||||
}
|
||||
|
||||
get width(): number {
|
||||
if (this.ios) {
|
||||
return this.ios.size.width;
|
||||
}
|
||||
|
||||
return NaN;
|
||||
}
|
||||
}
|
||||
|
||||
function getImageData(instance: UIImage, format: string, quality = 1.0): NSData {
|
||||
var data = null;
|
||||
switch (format) {
|
||||
case enums.ImageFormat.png: // PNG
|
||||
data = UIImagePNGRepresentation(instance);
|
||||
break;
|
||||
case enums.ImageFormat.jpeg: // JPEG
|
||||
data = UIImageJPEGRepresentation(instance, quality);
|
||||
break;
|
||||
|
||||
}
|
||||
return data;
|
||||
}
|
Reference in New Issue
Block a user