From 4d8705ad390d05104ed449868499cfc3d6659852 Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Wed, 15 Feb 2023 12:24:56 -0300 Subject: [PATCH] feat: allow modifying image scale on iOS --- packages/core/image-source/index.android.ts | 10 +++++++++- packages/core/image-source/index.d.ts | 11 +++++++++++ packages/core/image-source/index.ios.ts | 13 ++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/packages/core/image-source/index.android.ts b/packages/core/image-source/index.android.ts index b331ad651a..a2d2344b31 100644 --- a/packages/core/image-source/index.android.ts +++ b/packages/core/image-source/index.android.ts @@ -1,5 +1,5 @@ // Definitions. -import { ImageSource as ImageSourceDefinition } from '.'; +import type { ImageSource as ImageSourceDefinition } from '.'; import { ImageAsset } from '../image-asset'; import * as httpModule from '../http'; @@ -69,6 +69,10 @@ export class ImageSource implements ImageSourceDefinition { this._rotationAngle = value; } + get scale(): number { + return NaN; + } + constructor(nativeSource?: any) { if (nativeSource) { this.setNativeSource(nativeSource); @@ -408,6 +412,10 @@ export class ImageSource implements ImageSourceDefinition { ); }); } + + public withScaleFactor(newScale: number): ImageSource { + return this; + } } function getTargetFormat(format: 'png' | 'jpeg' | 'jpg'): android.graphics.Bitmap.CompressFormat { diff --git a/packages/core/image-source/index.d.ts b/packages/core/image-source/index.d.ts index 55037abc92..7631d5aafd 100644 --- a/packages/core/image-source/index.d.ts +++ b/packages/core/image-source/index.d.ts @@ -20,6 +20,11 @@ export class ImageSource { */ rotationAngle: number; + /** + * Gets the scale factor of the image (iOS only). + */ + readonly scale: number; + /** * The iOS-specific [UIImage](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/) instance. Will be undefined when running on Android. */ @@ -245,6 +250,12 @@ export class ImageSource { * bilinear filtering is typically minimal and the improved image quality is significant. */ resizeAsync(maxSize: number, options?: any): Promise; + + /** + * iOS Only. Creates a new ImageSource from the original UIImage but with a different scale factor. + * @param newScale The new scale of the image. + */ + withScaleFactor(newScale: number): ImageSource; } /** diff --git a/packages/core/image-source/index.ios.ts b/packages/core/image-source/index.ios.ts index fd8fbf6df4..81d27fabdf 100644 --- a/packages/core/image-source/index.ios.ts +++ b/packages/core/image-source/index.ios.ts @@ -1,5 +1,5 @@ // Definitions. -import { ImageSource as ImageSourceDefinition } from '.'; +import type { ImageSource as ImageSourceDefinition } from '.'; import { ImageAsset } from '../image-asset'; import * as httpModule from '../http'; import { Font } from '../ui/styling/font'; @@ -49,6 +49,13 @@ export class ImageSource implements ImageSourceDefinition { // compatibility with Android } + get scale(): number { + if (this.ios) { + return this.ios.scale; + } + return NaN; + } + constructor(nativeSource?: any) { if (nativeSource) { this.setNativeSource(nativeSource); @@ -434,6 +441,10 @@ export class ImageSource implements ImageSourceDefinition { }); }); } + + public withScaleFactor(newScale: number): ImageSource { + return new ImageSource(UIImage.imageWithCGImageScaleOrientation(this.ios.CGImage, newScale, this.ios.imageOrientation)); + } } function getFileName(path: string): string {