From 81e63ee19e498e5df3128996f532cd7b9863734c Mon Sep 17 00:00:00 2001 From: Alexander Djenkov Date: Tue, 31 Jul 2018 10:12:27 +0300 Subject: [PATCH] feat(image-asset-ios): add autoScaleFactor option to switch auto scaling (#6127) --- tests/app/image-source/image-source-tests.ts | 25 ++++++++++++++++--- .../image-asset/image-asset-common.ts | 6 ++--- tns-core-modules/image-asset/image-asset.d.ts | 1 + .../image-asset/image-asset.ios.ts | 9 ++++--- .../image-source/image-source.ios.ts | 2 +- 5 files changed, 32 insertions(+), 11 deletions(-) diff --git a/tests/app/image-source/image-source-tests.ts b/tests/app/image-source/image-source-tests.ts index 671ba77b2..be4e630cc 100644 --- a/tests/app/image-source/image-source-tests.ts +++ b/tests/app/image-source/image-source-tests.ts @@ -111,20 +111,37 @@ export function testFromAssetSimple(done) { }); } -export function testFromAssetWithScaling(done) { +export function testFromAssetWithExactScaling(done) { let asset = new imageAssetModule.ImageAsset(splashscreenPath); let scaleWidth = 10; let scaleHeight = 11; asset.options = { width: scaleWidth, height: scaleHeight, - keepAspectRatio: false + keepAspectRatio: false, + autoScaleFactor: false }; - let img = imageSource.fromAsset(asset).then((source) => { + imageSource.fromAsset(asset).then((source) => { TKUnit.assertEqual(source.width, scaleWidth); TKUnit.assertEqual(source.height, scaleHeight); - done(); + + const targetFilename = `splashscreenTemp.png`; + const tempPath = fs.knownFolders.temp().path; + const localFullPath = fs.path.join(tempPath, targetFilename); + + const fullImageSaved = source.saveToFile(localFullPath, "png"); + + if (fullImageSaved) { + let sourceImage = new imageSource.ImageSource(); + sourceImage.fromFile(localFullPath).then(() => { + TKUnit.assertEqual(sourceImage.width, scaleWidth); + TKUnit.assertEqual(sourceImage.height, scaleHeight); + done(); + }); + } else { + done(`Error saving photo to local temp folder: ${localFullPath}`); + } }, (error) => { done(error); }); diff --git a/tns-core-modules/image-asset/image-asset-common.ts b/tns-core-modules/image-asset/image-asset-common.ts index 52cbb5a42..0bed79c2c 100644 --- a/tns-core-modules/image-asset/image-asset-common.ts +++ b/tns-core-modules/image-asset/image-asset-common.ts @@ -2,16 +2,16 @@ import * as definition from "."; import * as observable from "../data/observable"; import * as platform from "../platform"; -export class ImageAsset extends observable.Observable implements definition.ImageAsset { +export class ImageAsset extends observable.Observable implements definition.ImageAsset { private _options: definition.ImageAssetOptions; private _nativeImage: any; ios: PHAsset; android: string; - constructor () { + constructor() { super(); - this._options = { keepAspectRatio: true }; + this._options = { keepAspectRatio: true, autoScaleFactor: true }; } get options(): definition.ImageAssetOptions { diff --git a/tns-core-modules/image-asset/image-asset.d.ts b/tns-core-modules/image-asset/image-asset.d.ts index d9d432d65..b21688040 100644 --- a/tns-core-modules/image-asset/image-asset.d.ts +++ b/tns-core-modules/image-asset/image-asset.d.ts @@ -17,4 +17,5 @@ export interface ImageAssetOptions { width?: number; height?: number; keepAspectRatio?: boolean; + autoScaleFactor?: boolean; } diff --git a/tns-core-modules/image-asset/image-asset.ios.ts b/tns-core-modules/image-asset/image-asset.ios.ts index d80750183..ed1548349 100644 --- a/tns-core-modules/image-asset/image-asset.ios.ts +++ b/tns-core-modules/image-asset/image-asset.ios.ts @@ -50,7 +50,7 @@ export class ImageAsset extends common.ImageAsset { let imageRequestOptions = PHImageRequestOptions.alloc().init(); imageRequestOptions.deliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat; imageRequestOptions.networkAccessAllowed = true; - + PHImageManager.defaultManager().requestImageForAssetTargetSizeContentModeOptionsResultHandler(this.ios, requestedSize, PHImageContentMode.AspectFit, imageRequestOptions, (image, imageResultInfo) => { if (image) { @@ -64,8 +64,11 @@ export class ImageAsset extends common.ImageAsset { ); } - private scaleImage(image: UIImage, requestedSize: {width: number, height: number}): UIImage { - UIGraphicsBeginImageContextWithOptions(requestedSize, false, 0.0); + private scaleImage(image: UIImage, requestedSize: { width: number, height: number }): UIImage { + // scaleFactor = 0 takes the scale factor of the devices's main screen. + const scaleFactor = this.options && this.options.autoScaleFactor === false ? 1.0 : 0.0; + + UIGraphicsBeginImageContextWithOptions(requestedSize, false, scaleFactor); image.drawInRect(CGRectMake(0, 0, requestedSize.width, requestedSize.height)); let resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); diff --git a/tns-core-modules/image-source/image-source.ios.ts b/tns-core-modules/image-source/image-source.ios.ts index 6c62c45bf..d4771f4f7 100644 --- a/tns-core-modules/image-source/image-source.ios.ts +++ b/tns-core-modules/image-source/image-source.ios.ts @@ -190,7 +190,7 @@ function getFileName(path: string): string { return fileName; } -function getImageData(instance: UIImage, format: "png" | "jpeg" | "jpg", quality = 1.0): NSData { +function getImageData(instance: UIImage, format: "png" | "jpeg" | "jpg", quality = 0.9): NSData { var data = null; switch (format) { case "png":