FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat(ios-image-source): standardize quality scale in image-source by sudhanshu-15 · Pull Request #5517 · NativeScript/NativeScript · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .ts  (3) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
31 changes: 31 additions & 0 deletions tests/app/image-source/image-source-tests.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);

Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions tns-core-modules/image-source/image-source.d.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
8 changes: 8 additions & 0 deletions tns-core-modules/image-source/image-source.ios.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down

Back | FazBrowse Home | New Git URL