import { TNSImageAssetBase , TNSImageAssetSaveFormat , } from './canvas-plugin.common' ; import * as fs from '@nativescript/core/file-system' ; declare var ImageAsset ; const main_queue = dispatch_get_current_queue ( ) ; export class TNSImageAsset extends TNSImageAssetBase { constructor ( ) { super ( ImageAsset . alloc ( ) . init ( ) ) ; } get width ( ) { return this . native . width ; } get height ( ) { return this . native . height ; } get error ( ) : string { return this . native . error ; } loadFile ( path ) { let realPath = path ; if ( typeof realPath === 'string' ) { if ( realPath . startsWith ( '~/' ) ) { realPath = fs . path . join ( fs . knownFolders . currentApp ( ) . path , realPath . replace ( '~/' , '' ) ) ; } } return this . native . loadImageFromPathWithPath ( realPath ) ; } loadFileAsync ( path ) { return new Promise ( ( resolve , reject ) => { const queue = this . _createQueue ( ) ; dispatch_async ( queue , ( ) => { const success = this . loadFile ( path ) ; dispatch_async ( main_queue , ( ) => { if ( ! success ) { reject ( this . error ) ; return ; } resolve ( true ) ; } ) ; } ) ; } ) ; } loadFromNative ( image ) { return this . native . loadImageFromImageWithImage ( image ) ; } loadFromNativeAsync ( image ) { return new Promise ( ( resolve , reject ) => { const queue = this . _createQueue ( ) ; dispatch_async ( queue , ( ) => { const success = this . loadFromNative ( image ) ; dispatch_async ( main_queue , ( ) => { if ( ! success ) { reject ( this . error ) ; return ; } resolve ( true ) ; } ) ; } ) ; } ) ; } loadFromBytes ( bytes ) { return this . native . loadImageFromBytesWithArray ( bytes as any ) ; } loadFromBytesAsync ( bytes ) { return new Promise ( ( resolve , reject ) => { const queue = this . _createQueue ( ) ; dispatch_async ( queue , ( ) => { const success = this . loadFromBytes ( bytes ) ; dispatch_async ( main_queue , ( ) => { if ( ! success ) { reject ( this . error ) ; return ; } resolve ( true ) ; } ) ; } ) ; } ) ; } scale ( x , y ) { this . native . scaleWithXY ( x , y ) ; } save ( path , format ) { return this . native . saveWithPathFormat ( path , format . valueOf ( ) ) ; } saveAsync ( path , format ) { return new Promise ( ( resolve , reject ) => { const result = this . save ( path , format ) ; dispatch_async ( main_queue , ( ) => { if ( ! result ) { reject ( this . error ) ; return ; } resolve ( result ) ; } ) ; } ) ; } flipX ( ) { this . native . flipX ( ) ; } flipY ( ) { this . native . flipY ( ) ; } private _createQueue ( ) { return dispatch_queue_create ( 'TNSImageAsset' , null ) ; } }