diff --git a/tns-core-modules/image-asset/image-asset-common.ts b/tns-core-modules/image-asset/image-asset-common.ts index aec25a5b6..e08382c78 100644 --- a/tns-core-modules/image-asset/image-asset-common.ts +++ b/tns-core-modules/image-asset/image-asset-common.ts @@ -56,23 +56,23 @@ export function getAspectSafeDimensions(sourceWidth, sourceHeight, reqWidth, req }; } -export function getRequestedImageSize(src: {width: number, height: number}): {width: number, height: number} { +export function getRequestedImageSize(src: { width: number, height: number }, options: definition.ImageAssetOptions): { width: number, height: number } { let reqWidth = platform.screen.mainScreen.widthDIPs; - let reqHeight = platform.screen.mainScreen.heightDIPs - if (this.options && this.options.width) { - reqWidth = (this.options.width > 0 && this.options.width < reqWidth) ? this.options.width : reqWidth; + let reqHeight = platform.screen.mainScreen.heightDIPs; + if (options && options.width) { + reqWidth = (options.width > 0 && options.width < reqWidth) ? options.width : reqWidth; } - if (this.options && this.options.height) { - reqWidth = (this.options.height > 0 && this.options.height < reqHeight) ? this.options.height : reqHeight; + if (options && options.height) { + reqHeight = (options.height > 0 && options.height < reqHeight) ? options.height : reqHeight; } - if (this.options && this.options.keepAspectRatio) { + if (options && options.keepAspectRatio) { let safeAspectSize = getAspectSafeDimensions(src.width, src.height, reqWidth, reqHeight); reqWidth = safeAspectSize.width; reqHeight = safeAspectSize.height; } - return { + return { width: reqWidth, - height:reqHeight + height: reqHeight }; } \ No newline at end of file diff --git a/tns-core-modules/image-asset/image-asset.android.ts b/tns-core-modules/image-asset/image-asset.android.ts index 187711c37..2db9f412c 100644 --- a/tns-core-modules/image-asset/image-asset.android.ts +++ b/tns-core-modules/image-asset/image-asset.android.ts @@ -17,7 +17,7 @@ export class ImageAsset extends common.ImageAsset { width: bitmapOptions.outWidth, height: bitmapOptions.outHeight }; - let requestedSize = common.getRequestedImageSize(sourceSize); + let requestedSize = common.getRequestedImageSize(sourceSize, this.options); let sampleSize = calculateInSampleSize(bitmapOptions.outWidth, bitmapOptions.outHeight, requestedSize.width, requestedSize.height); diff --git a/tns-core-modules/image-asset/image-asset.ios.ts b/tns-core-modules/image-asset/image-asset.ios.ts index e7bc8f52d..9e7c9f170 100644 --- a/tns-core-modules/image-asset/image-asset.ios.ts +++ b/tns-core-modules/image-asset/image-asset.ios.ts @@ -14,17 +14,13 @@ export class ImageAsset extends common.ImageAsset { } public getImageAsync(callback: (image, error) => void) { - let requestedSize = common.getRequestedImageSize({ - width: this.nativeImage ? this.nativeImage.size.width : this.ios.pixelWidth, - height: this.nativeImage ? this.nativeImage.size.height : this.ios.pixelHeight - }); + let srcWidth = this.nativeImage ? this.nativeImage.size.width : this.ios.pixelWidth; + let srcHeight = this.nativeImage ? this.nativeImage.size.height : this.ios.pixelHeight; + let requestedSize = common.getRequestedImageSize({ width: srcWidth, height: srcHeight }, this.options); if (this.nativeImage) { let newSize = CGSizeMake(requestedSize.width, requestedSize.height); - UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0); - this.nativeImage.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height)); - let resizedImage = UIGraphicsGetImageFromCurrentImageContext(); - UIGraphicsEndImageContext(); + let resizedImage = this.scaleImage(this.nativeImage, newSize); callback(resizedImage, null); return; } @@ -35,7 +31,8 @@ export class ImageAsset extends common.ImageAsset { PHImageManager.defaultManager().requestImageForAssetTargetSizeContentModeOptionsResultHandler(this.ios, requestedSize, PHImageContentMode.AspectFit, imageRequestOptions, (image, imageResultInfo) => { if (image) { - callback(image, null); + let resultImage = this.scaleImage(image, requestedSize); + callback(resultImage, null); } else { callback(null, imageResultInfo.valueForKey(PHImageErrorKey)); @@ -43,4 +40,12 @@ export class ImageAsset extends common.ImageAsset { } ); } + + private scaleImage(image: UIImage, requestedSize: {width: number, height: number}): UIImage { + UIGraphicsBeginImageContextWithOptions(requestedSize, false, 0.0); + image.drawInRect(CGRectMake(0, 0, requestedSize.width, requestedSize.height)); + let resultImage = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + return resultImage; + } } \ No newline at end of file