mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-15 11:01:21 +08:00
Fixed issue with image resizing.
This commit is contained in:
@ -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
|
||||
};
|
||||
}
|
@ -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);
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user