mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
feat(ios-image-source): standardize quality scale in image-source (#5517)
* feat(ios-image-source): standardize quality scale in image-source for both platforms Normalize quality in saveToFile and toBase64String to follow 0-100 scale - standardize implementation on both platforms closes #5474
This commit is contained in:
committed by
Alexander Vakrilov
parent
e1a1d643c8
commit
319c153360
@@ -54,6 +54,15 @@ export function testSaveToFile() {
|
||||
TKUnit.assert(fs.File.exists(path), "Image not saved to file");
|
||||
}
|
||||
|
||||
export function testSaveToFile_WithQuality() {
|
||||
const img = imageSource.fromFile(imagePath);
|
||||
const folder = fs.knownFolders.documents();
|
||||
const path = fs.path.join(folder.path, "test.png");
|
||||
const saved = img.saveToFile(path, "png", 70);
|
||||
TKUnit.assert(saved, "Image not saved to file");
|
||||
TKUnit.assert(fs.File.exists(path), "Image not saved to file");
|
||||
}
|
||||
|
||||
export function testFromFile() {
|
||||
// >> imagesource-load-local
|
||||
const folder = fs.knownFolders.documents();
|
||||
@@ -187,6 +196,16 @@ export function testBase64Encode_PNG() {
|
||||
"Base 64 encoded PNG");
|
||||
}
|
||||
|
||||
export function testBase64Encode_PNG_WithQuality() {
|
||||
const img = imageSource.fromFile(smallImagePath);
|
||||
let base64String = img.toBase64String("png", 80);
|
||||
base64String = base64String.substr(0, expectedPngStart.length);
|
||||
TKUnit.assertEqual(
|
||||
base64String,
|
||||
expectedPngStart,
|
||||
"Base 64 encoded PNG");
|
||||
}
|
||||
|
||||
export function testBase64Encode_JPEG() {
|
||||
const img = imageSource.fromFile(smallImagePath);
|
||||
|
||||
@@ -199,6 +218,18 @@ export function testBase64Encode_JPEG() {
|
||||
"Base 64 encoded JPEG");
|
||||
}
|
||||
|
||||
export function testBase64Encode_JPEG_With_Quality() {
|
||||
const img = imageSource.fromFile(smallImagePath);
|
||||
|
||||
let base64String = img.toBase64String("jpeg", 80);
|
||||
base64String = base64String.substr(0, expectedJpegStart.length);
|
||||
|
||||
TKUnit.assertEqual(
|
||||
base64String,
|
||||
expectedJpegStart,
|
||||
"Base 64 encoded JPEG");
|
||||
}
|
||||
|
||||
export function testLoadFromBase64Encode_JPEG() {
|
||||
// >> imagesource-from-base-string
|
||||
let img: imageSource.ImageSource;
|
||||
|
||||
@@ -98,14 +98,14 @@ export class ImageSource {
|
||||
* Saves this instance to the specified file, using the provided image format and quality.
|
||||
* @param path The path of the file on the file system to save to.
|
||||
* @param format The format (encoding) of the image.
|
||||
* @param quality Optional parameter, specifying the quality of the encoding. Defaults to the maximum available quality.
|
||||
* @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.
|
||||
*/
|
||||
saveToFile(path: string, format: "png" | "jpeg" | "jpg", quality?: number): boolean;
|
||||
|
||||
/**
|
||||
* Converts the image to base64 encoded string, using the provided image format and quality.
|
||||
* @param format The format (encoding) of the image.
|
||||
* @param quality Optional parameter, specifying the quality of the encoding. Defaults to the maximum available quality.
|
||||
* @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;
|
||||
}
|
||||
|
||||
@@ -130,6 +130,10 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (quality) {
|
||||
quality = quality - 0 / (100 - 0); // Normalize quality on a scale of 0 to 1
|
||||
}
|
||||
|
||||
const data = getImageData(this.ios, format, quality);
|
||||
if (data) {
|
||||
return NSFileManager.defaultManager.createFileAtPathContentsAttributes(path, data, null);
|
||||
@@ -144,6 +148,10 @@ export class ImageSource implements ImageSourceDefinition {
|
||||
return res;
|
||||
}
|
||||
|
||||
if (quality) {
|
||||
quality = quality - 0 / (100 - 0); // Normalize quality on a scale of 0 to 1
|
||||
}
|
||||
|
||||
const data = getImageData(this.ios, format, quality);
|
||||
if (data) {
|
||||
res = data.base64Encoding();
|
||||
|
||||
Reference in New Issue
Block a user