Fixed issue with image resizing.

This commit is contained in:
Nedyalko Nikolov
2016-10-20 17:25:08 +03:00
parent 6abb0db99b
commit 23352338d2
3 changed files with 24 additions and 19 deletions

View File

@ -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 reqWidth = platform.screen.mainScreen.widthDIPs;
let reqHeight = platform.screen.mainScreen.heightDIPs let reqHeight = platform.screen.mainScreen.heightDIPs;
if (this.options && this.options.width) { if (options && options.width) {
reqWidth = (this.options.width > 0 && this.options.width < reqWidth) ? this.options.width : reqWidth; reqWidth = (options.width > 0 && options.width < reqWidth) ? options.width : reqWidth;
} }
if (this.options && this.options.height) { if (options && options.height) {
reqWidth = (this.options.height > 0 && this.options.height < reqHeight) ? this.options.height : reqHeight; 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); let safeAspectSize = getAspectSafeDimensions(src.width, src.height, reqWidth, reqHeight);
reqWidth = safeAspectSize.width; reqWidth = safeAspectSize.width;
reqHeight = safeAspectSize.height; reqHeight = safeAspectSize.height;
} }
return { return {
width: reqWidth, width: reqWidth,
height:reqHeight height: reqHeight
}; };
} }

View File

@ -17,7 +17,7 @@ export class ImageAsset extends common.ImageAsset {
width: bitmapOptions.outWidth, width: bitmapOptions.outWidth,
height: bitmapOptions.outHeight 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); let sampleSize = calculateInSampleSize(bitmapOptions.outWidth, bitmapOptions.outHeight, requestedSize.width, requestedSize.height);

View File

@ -14,17 +14,13 @@ export class ImageAsset extends common.ImageAsset {
} }
public getImageAsync(callback: (image, error) => void) { public getImageAsync(callback: (image, error) => void) {
let requestedSize = common.getRequestedImageSize({ let srcWidth = this.nativeImage ? this.nativeImage.size.width : this.ios.pixelWidth;
width: this.nativeImage ? this.nativeImage.size.width : this.ios.pixelWidth, let srcHeight = this.nativeImage ? this.nativeImage.size.height : this.ios.pixelHeight;
height: this.nativeImage ? this.nativeImage.size.height : this.ios.pixelHeight let requestedSize = common.getRequestedImageSize({ width: srcWidth, height: srcHeight }, this.options);
});
if (this.nativeImage) { if (this.nativeImage) {
let newSize = CGSizeMake(requestedSize.width, requestedSize.height); let newSize = CGSizeMake(requestedSize.width, requestedSize.height);
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0); let resizedImage = this.scaleImage(this.nativeImage, newSize);
this.nativeImage.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height));
let resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
callback(resizedImage, null); callback(resizedImage, null);
return; return;
} }
@ -35,7 +31,8 @@ export class ImageAsset extends common.ImageAsset {
PHImageManager.defaultManager().requestImageForAssetTargetSizeContentModeOptionsResultHandler(this.ios, requestedSize, PHImageContentMode.AspectFit, imageRequestOptions, PHImageManager.defaultManager().requestImageForAssetTargetSizeContentModeOptionsResultHandler(this.ios, requestedSize, PHImageContentMode.AspectFit, imageRequestOptions,
(image, imageResultInfo) => { (image, imageResultInfo) => {
if (image) { if (image) {
callback(image, null); let resultImage = this.scaleImage(image, requestedSize);
callback(resultImage, null);
} }
else { else {
callback(null, imageResultInfo.valueForKey(PHImageErrorKey)); 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;
}
} }