1 | import { ImageAssetBase, getRequestedImageSize } from './image-asset-common';
|
2 | import { path as fsPath, knownFolders } from '../file-system';
|
3 | import { queueGC } from '../utils';
|
4 | export * from './image-asset-common';
|
5 | export class ImageAsset extends ImageAssetBase {
|
6 | constructor(asset) {
|
7 | super();
|
8 | if (typeof asset === 'string') {
|
9 | if (asset.indexOf('~/') === 0) {
|
10 | asset = fsPath.join(knownFolders.currentApp().path, asset.replace('~/', ''));
|
11 | }
|
12 | this.nativeImage = UIImage.imageWithContentsOfFile(asset);
|
13 | }
|
14 | else if (asset instanceof UIImage) {
|
15 | this.nativeImage = asset;
|
16 | }
|
17 | else {
|
18 | this.ios = asset;
|
19 | }
|
20 | }
|
21 |
|
22 | get ios() {
|
23 | return this._ios;
|
24 | }
|
25 | set ios(value) {
|
26 | this._ios = value;
|
27 | }
|
28 | getImageAsync(callback) {
|
29 | if (!this.ios && !this.nativeImage) {
|
30 | callback(null, 'Asset cannot be found.');
|
31 | }
|
32 | const srcWidth = this.nativeImage ? this.nativeImage.size.width : this.ios.pixelWidth;
|
33 | const srcHeight = this.nativeImage ? this.nativeImage.size.height : this.ios.pixelHeight;
|
34 | const requestedSize = getRequestedImageSize({ width: srcWidth, height: srcHeight }, this.options);
|
35 | if (this.nativeImage) {
|
36 | callback(this.scaleImage(this.nativeImage, CGSizeMake(requestedSize.width, requestedSize.height)), null);
|
37 | queueGC();
|
38 | return;
|
39 | }
|
40 | const imageRequestOptions = PHImageRequestOptions.alloc().init();
|
41 | imageRequestOptions.deliveryMode = 1 ;
|
42 | imageRequestOptions.networkAccessAllowed = true;
|
43 | PHImageManager.defaultManager().requestImageForAssetTargetSizeContentModeOptionsResultHandler(this.ios, requestedSize, 0 , imageRequestOptions, (image, imageResultInfo) => {
|
44 | if (image) {
|
45 | callback(this.scaleImage(image, requestedSize), null);
|
46 | }
|
47 | else {
|
48 | callback(null, imageResultInfo.valueForKey(PHImageErrorKey));
|
49 | }
|
50 | queueGC();
|
51 | });
|
52 | }
|
53 | scaleImage(image, requestedSize) {
|
54 | return NativeScriptUtils.scaleImageWidthHeightScaleFactor(image, requestedSize.width, requestedSize.height, this.options?.autoScaleFactor === false ? 1.0 : 0.0);
|
55 | }
|
56 | }
|
57 |
|
\ | No newline at end of file |