mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
feat(ImageSource): resize method (#8678)
This commit is contained in:
@@ -1198,6 +1198,8 @@ export class ImageSource {
|
||||
// @deprecated (undocumented)
|
||||
loadFromResource(name: string): boolean;
|
||||
|
||||
resize(maxSize: number, options?: any): ImageSource;
|
||||
|
||||
rotationAngle: number;
|
||||
|
||||
saveToFile(path: string, format: "png" | "jpeg" | "jpg", quality?: number): boolean;
|
||||
|
||||
27
nativescript-core/image-source/image-source-common.ts
Normal file
27
nativescript-core/image-source/image-source-common.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
export function getScaledDimensions(
|
||||
width: number,
|
||||
height: number,
|
||||
maxSize: number
|
||||
) {
|
||||
if (height >= width) {
|
||||
if (height <= maxSize) {
|
||||
// if image already smaller than the required height
|
||||
return { width, height };
|
||||
}
|
||||
|
||||
return {
|
||||
width: Math.round((maxSize * width) / height),
|
||||
height: maxSize
|
||||
};
|
||||
}
|
||||
|
||||
if (width <= maxSize) {
|
||||
// if image already smaller than the required width
|
||||
return { width, height };
|
||||
}
|
||||
|
||||
return {
|
||||
width: maxSize,
|
||||
height: Math.round((maxSize * height) / width)
|
||||
};
|
||||
}
|
||||
@@ -10,6 +10,8 @@ import { getNativeApplication } from "../application";
|
||||
import { Font } from "../ui/styling/font";
|
||||
import { Color } from "../color";
|
||||
|
||||
import { getScaledDimensions } from "./image-source-common";
|
||||
|
||||
export { isFileOrResourcePath };
|
||||
|
||||
let http: typeof httpModule;
|
||||
@@ -337,6 +339,18 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
|
||||
return outputStream.toString();
|
||||
}
|
||||
|
||||
public resize(maxSize: number, options?: any): ImageSource {
|
||||
const dim = getScaledDimensions(this.android.getWidth(), this.android.getHeight(), maxSize);
|
||||
const bm: android.graphics.Bitmap = android.graphics.Bitmap.createScaledBitmap(
|
||||
this.android,
|
||||
dim.width,
|
||||
dim.height,
|
||||
options && options.filter
|
||||
);
|
||||
|
||||
return new ImageSource(bm);
|
||||
}
|
||||
}
|
||||
|
||||
function getTargetFormat(format: "png" | "jpeg" | "jpg"): android.graphics.Bitmap.CompressFormat {
|
||||
|
||||
13
nativescript-core/image-source/image-source.d.ts
vendored
13
nativescript-core/image-source/image-source.d.ts
vendored
@@ -209,6 +209,19 @@ export class ImageSource {
|
||||
* @param quality Optional parameter, specifying the quality of the encoding. Defaults to the maximum available quality. Quality varies on a scale of 0 to 100.
|
||||
*/
|
||||
toBase64String(format: "png" | "jpeg" | "jpg", quality?: number): string;
|
||||
|
||||
/**
|
||||
* Returns a new ImageSource that is a resized version of this image with the same aspect ratio, but the max dimension set to the provided maxSize.
|
||||
* @param maxSize The maximum pixel dimension of the resulting image.
|
||||
* @param options Optional parameter, Only used for android, options.filter is a boolean which
|
||||
* determines whether or not bilinear filtering should be used when scaling the bitmap.
|
||||
* If this is true then bilinear filtering will be used when scaling which has
|
||||
* better image quality at the cost of worse performance. If this is false then
|
||||
* nearest-neighbor scaling is used instead which will have worse image quality
|
||||
* but is faster. Recommended default is to set filter to 'true' as the cost of
|
||||
* bilinear filtering is typically minimal and the improved image quality is significant.
|
||||
*/
|
||||
resize(maxSize: number, options?: any): ImageSource;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -9,6 +9,8 @@ import { Color } from "../color";
|
||||
import { path as fsPath, knownFolders } from "../file-system";
|
||||
import { isFileOrResourcePath, RESOURCE_PREFIX, layout } from "../utils/utils";
|
||||
|
||||
import { getScaledDimensions } from "./image-source-common";
|
||||
|
||||
export { isFileOrResourcePath };
|
||||
|
||||
let http: typeof httpModule;
|
||||
@@ -336,6 +338,24 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
public resize(maxSize: number, options?: any): ImageSource {
|
||||
const size: CGSize = this.ios.size;
|
||||
const dim = getScaledDimensions(
|
||||
size.width,
|
||||
size.height,
|
||||
maxSize
|
||||
);
|
||||
|
||||
const newSize: CGSize = CGSizeMake(dim.width, dim.height);
|
||||
UIGraphicsBeginImageContextWithOptions(newSize, true, this.ios.scale);
|
||||
this.ios.drawInRect(CGRectMake(0, 0, newSize.width, newSize.height));
|
||||
|
||||
const resizedImage = UIGraphicsGetImageFromCurrentImageContext();
|
||||
UIGraphicsEndImageContext();
|
||||
|
||||
return new ImageSource(resizedImage);
|
||||
}
|
||||
}
|
||||
|
||||
function getFileName(path: string): string {
|
||||
|
||||
@@ -295,3 +295,13 @@ export function testLoadFromFontIconCode() {
|
||||
TKUnit.assert(img.width !== null, "img.width");
|
||||
TKUnit.assert(img.height !== null, "img.width");
|
||||
}
|
||||
|
||||
export function testResize() {
|
||||
const img = ImageSource.fromFileSync(imagePath);
|
||||
|
||||
const newSize = Math.floor(Math.max(img.width, img.height) / 2);
|
||||
|
||||
const resized = img.resize(newSize);
|
||||
|
||||
TKUnit.assert(resized.width === newSize || resized.height === newSize, "Image not resized correctly");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user