1
1
// Definitions.
2
- import { ImageSource as ImageSourceDefinition } from '.' ;
2
+ import type { ImageSource as ImageSourceDefinition , ImageSourceLoadOptions } from '.' ;
3
3
import { ImageAsset } from '../image-asset' ;
4
4
import * as httpModule from '../http' ;
5
5
import { Font } from '../ui/styling/font' ;
@@ -73,51 +73,77 @@ export class ImageSource implements ImageSourceDefinition {
73
73
return http . getImage ( url ) ;
74
74
}
75
75
76
- static fromResourceSync ( name : string ) : ImageSource {
77
- const nativeSource = ( < any > UIImage ) . tns_safeImageNamed ( name ) || ( < any > UIImage ) . tns_safeImageNamed ( `${ name } .jpg` ) ;
76
+ static fromResourceSync ( name : string , options ?: ImageSourceLoadOptions ) : ImageSource {
77
+ const scale = options ?. ios ?. scale ;
78
+ const nativeSource = typeof scale === 'number' ? ( < any > UIImage ) . tns_safeImageNamedScale ( name , scale ) || ( < any > UIImage ) . tns_safeImageNamedScale ( `${ name } .jpg` , scale ) : ( < any > UIImage ) . tns_safeImageNamed ( name ) || ( < any > UIImage ) . tns_safeImageNamed ( `${ name } .jpg` ) ;
78
79
79
80
return nativeSource ? new ImageSource ( nativeSource ) : null ;
80
81
}
81
- static fromResource ( name : string ) : Promise < ImageSource > {
82
+ static fromResource ( name : string , options ?: ImageSourceLoadOptions ) : Promise < ImageSource > {
83
+ const scale = options ?. ios ?. scale ;
82
84
return new Promise < ImageSource > ( ( resolve , reject ) => {
83
85
try {
84
- ( < any > UIImage ) . tns_safeDecodeImageNamedCompletion ( name , ( image ) => {
85
- if ( image ) {
86
- resolve ( new ImageSource ( image ) ) ;
87
- } else {
88
- ( < any > UIImage ) . tns_safeDecodeImageNamedCompletion ( `${ name } .jpg` , ( img ) => {
89
- if ( img ) {
90
- resolve ( new ImageSource ( img ) ) ;
91
- }
92
- } ) ;
93
- }
94
- } ) ;
86
+ if ( typeof scale === 'number' ) {
87
+ ( < any > UIImage ) . tns_safeDecodeImageNamedScaleCompletion ( name , ( image ) => {
88
+ if ( image ) {
89
+ resolve ( new ImageSource ( image ) ) ;
90
+ } else {
91
+ ( < any > UIImage ) . tns_safeDecodeImageNamedScaleCompletion ( `${ name } .jpg` , ( img ) => {
92
+ if ( img ) {
93
+ resolve ( new ImageSource ( img ) ) ;
94
+ }
95
+ } ) ;
96
+ }
97
+ } ) ;
98
+ } else {
99
+ ( < any > UIImage ) . tns_safeDecodeImageNamedCompletion ( name , ( image ) => {
100
+ if ( image ) {
101
+ resolve ( new ImageSource ( image ) ) ;
102
+ } else {
103
+ ( < any > UIImage ) . tns_safeDecodeImageNamedCompletion ( `${ name } .jpg` , ( img ) => {
104
+ if ( img ) {
105
+ resolve ( new ImageSource ( img ) ) ;
106
+ }
107
+ } ) ;
108
+ }
109
+ } ) ;
110
+ }
95
111
} catch ( ex ) {
96
112
reject ( ex ) ;
97
113
}
98
114
} ) ;
99
115
}
100
116
101
- static fromFileSync ( path : string ) : ImageSource {
102
- const uiImage = UIImage . imageWithContentsOfFile ( getFileName ( path ) ) ;
117
+ static fromFileSync ( path : string , options ?: ImageSourceLoadOptions ) : ImageSource {
118
+ const scale = options ?. ios ?. scale ;
119
+ const uiImage = typeof scale === 'number' ? UIImage . imageWithDataScale ( NSData . dataWithContentsOfFile ( getFileName ( path ) ) , scale ) : UIImage . imageWithContentsOfFile ( getFileName ( path ) ) ;
103
120
104
121
return uiImage ? new ImageSource ( uiImage ) : null ;
105
122
}
106
- static fromFile ( path : string ) : Promise < ImageSource > {
123
+ static fromFile ( path : string , options ?: ImageSourceLoadOptions ) : Promise < ImageSource > {
124
+ const scale = options ?. ios ?. scale ;
107
125
return new Promise < ImageSource > ( ( resolve , reject ) => {
108
126
try {
109
- ( < any > UIImage ) . tns_decodeImageWidthContentsOfFileCompletion ( getFileName ( path ) , ( uiImage ) => {
110
- if ( uiImage ) {
111
- resolve ( new ImageSource ( uiImage ) ) ;
112
- }
113
- } ) ;
127
+ if ( typeof scale === 'number' ) {
128
+ ( < any > UIImage ) . tns_decodeImageWidthContentsOfFileScaleCompletion ( getFileName ( path ) , scale , ( uiImage ) => {
129
+ if ( uiImage ) {
130
+ resolve ( new ImageSource ( uiImage ) ) ;
131
+ }
132
+ } ) ;
133
+ } else {
134
+ ( < any > UIImage ) . tns_decodeImageWidthContentsOfFileCompletion ( getFileName ( path ) , ( uiImage ) => {
135
+ if ( uiImage ) {
136
+ resolve ( new ImageSource ( uiImage ) ) ;
137
+ }
138
+ } ) ;
139
+ }
114
140
} catch ( ex ) {
115
141
reject ( ex ) ;
116
142
}
117
143
} ) ;
118
144
}
119
145
120
- static fromFileOrResourceSync ( path : string ) : ImageSource {
146
+ static fromFileOrResourceSync ( path : string , options ?: ImageSourceLoadOptions ) : ImageSource {
121
147
if ( ! isFileOrResourcePath ( path ) ) {
122
148
if ( Trace . isEnabled ( ) ) {
123
149
Trace . write ( 'Path "' + path + '" is not a valid file or resource.' , Trace . categories . Binding , Trace . messageType . error ) ;
@@ -126,48 +152,64 @@ export class ImageSource implements ImageSourceDefinition {
126
152
}
127
153
128
154
if ( path . indexOf ( RESOURCE_PREFIX ) === 0 ) {
129
- return ImageSource . fromResourceSync ( path . substr ( RESOURCE_PREFIX . length ) ) ;
155
+ return ImageSource . fromResourceSync ( path . substr ( RESOURCE_PREFIX . length ) , options ) ;
130
156
}
131
157
132
- return ImageSource . fromFileSync ( path ) ;
158
+ return ImageSource . fromFileSync ( path , options ) ;
133
159
}
134
160
135
- static fromDataSync ( data : any ) : ImageSource {
136
- const uiImage = UIImage . imageWithData ( data ) ;
161
+ static fromDataSync ( data : any , options ?: ImageSourceLoadOptions ) : ImageSource {
162
+ const scale = options ?. ios ?. scale ;
163
+ const uiImage = typeof scale === 'number' ? UIImage . imageWithDataScale ( data , scale ) : UIImage . imageWithData ( data ) ;
137
164
138
165
return uiImage ? new ImageSource ( uiImage ) : null ;
139
166
}
140
- static fromData ( data : any ) : Promise < ImageSource > {
167
+ static fromData ( data : any , options ?: ImageSourceLoadOptions ) : Promise < ImageSource > {
168
+ const scale = options ?. ios ?. scale ;
141
169
return new Promise < ImageSource > ( ( resolve , reject ) => {
142
170
try {
143
- ( < any > UIImage ) . tns_decodeImageWithDataCompletion ( data , ( uiImage ) => {
144
- if ( uiImage ) {
145
- resolve ( new ImageSource ( uiImage ) ) ;
146
- }
147
- } ) ;
171
+ if ( typeof scale === 'number' ) {
172
+ ( < any > UIImage ) . tns_decodeImageWithDataScaleCompletion ( data , scale , ( uiImage ) => {
173
+ if ( uiImage ) {
174
+ resolve ( new ImageSource ( uiImage ) ) ;
175
+ }
176
+ } ) ;
177
+ } else {
178
+ ( < any > UIImage ) . tns_decodeImageWithDataCompletion ( data , ( uiImage ) => {
179
+ if ( uiImage ) {
180
+ resolve ( new ImageSource ( uiImage ) ) ;
181
+ }
182
+ } ) ;
183
+ }
148
184
} catch ( ex ) {
149
185
reject ( ex ) ;
150
186
}
151
187
} ) ;
152
188
}
153
189
154
- static fromBase64Sync ( source : string ) : ImageSource {
190
+ static fromBase64Sync ( source : string , options ?: ImageSourceLoadOptions ) : ImageSource {
191
+ const scale = options ?. ios ?. scale ;
155
192
let uiImage : UIImage ;
156
193
if ( typeof source === 'string' ) {
157
194
const data = NSData . alloc ( ) . initWithBase64EncodedStringOptions ( source , NSDataBase64DecodingOptions . IgnoreUnknownCharacters ) ;
158
- uiImage = UIImage . imageWithData ( data ) ;
195
+ if ( typeof scale === 'number' ) {
196
+ uiImage = UIImage . imageWithDataScale ( data , scale ) ;
197
+ } else {
198
+ uiImage = UIImage . imageWithData ( data ) ;
199
+ }
159
200
}
160
201
161
202
return uiImage ? new ImageSource ( uiImage ) : null ;
162
203
}
163
- static fromBase64 ( source : string ) : Promise < ImageSource > {
204
+ static fromBase64 ( source : string , options ?: ImageSourceLoadOptions ) : Promise < ImageSource > {
205
+ const scale = options ?. ios ?. scale ;
164
206
return new Promise < ImageSource > ( ( resolve , reject ) => {
165
207
try {
166
208
const data = NSData . alloc ( ) . initWithBase64EncodedStringOptions ( source , NSDataBase64DecodingOptions . IgnoreUnknownCharacters ) ;
167
209
const main_queue = dispatch_get_current_queue ( ) ;
168
210
const background_queue = dispatch_get_global_queue ( qos_class_t . QOS_CLASS_DEFAULT , 0 ) ;
169
211
dispatch_async ( background_queue , ( ) => {
170
- const uiImage = UIImage . imageWithData ( data ) ;
212
+ const uiImage = typeof scale === 'number' ? UIImage . imageWithDataScale ( data , scale ) : UIImage . imageWithData ( data ) ;
171
213
dispatch_async ( main_queue , ( ) => {
172
214
resolve ( new ImageSource ( uiImage ) ) ;
173
215
} ) ;
@@ -178,8 +220,9 @@ export class ImageSource implements ImageSourceDefinition {
178
220
} ) ;
179
221
}
180
222
181
- static fromFontIconCodeSync ( source : string , font : Font , color : Color ) : ImageSource {
223
+ static fromFontIconCodeSync ( source : string , font : Font , color : Color , options ?: ImageSourceLoadOptions ) : ImageSource {
182
224
font = font || Font . default ;
225
+ const scale = typeof options ?. ios ?. scale === 'number' ? options . ios . scale : 0.0 ;
183
226
184
227
// TODO: Consider making 36 font size as default for optimal look on TabView and ActionBar
185
228
const attributes = {
@@ -192,7 +235,7 @@ export class ImageSource implements ImageSourceDefinition {
192
235
193
236
const attributedString = NSAttributedString . alloc ( ) . initWithStringAttributes ( source , < NSDictionary < string , any > > attributes ) ;
194
237
195
- UIGraphicsBeginImageContextWithOptions ( attributedString . size ( ) , false , 0.0 ) ;
238
+ UIGraphicsBeginImageContextWithOptions ( attributedString . size ( ) , false , scale ) ;
196
239
attributedString . drawAtPoint ( CGPointMake ( 0 , 0 ) ) ;
197
240
198
241
const iconImage = UIGraphicsGetImageFromCurrentImageContext ( ) ;
0 commit comments