Skip to content

Commit 319c153

Browse files
sudhanshu-15Alexander Vakrilov
authored andcommitted
feat(ios-image-source): standardize quality scale in image-source (NativeScript#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 NativeScript#5474
1 parent e1a1d64 commit 319c153

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

tests/app/image-source/image-source-tests.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,15 @@ export function testSaveToFile() {
5454
TKUnit.assert(fs.File.exists(path), "Image not saved to file");
5555
}
5656

57+
export function testSaveToFile_WithQuality() {
58+
const img = imageSource.fromFile(imagePath);
59+
const folder = fs.knownFolders.documents();
60+
const path = fs.path.join(folder.path, "test.png");
61+
const saved = img.saveToFile(path, "png", 70);
62+
TKUnit.assert(saved, "Image not saved to file");
63+
TKUnit.assert(fs.File.exists(path), "Image not saved to file");
64+
}
65+
5766
export function testFromFile() {
5867
// >> imagesource-load-local
5968
const folder = fs.knownFolders.documents();
@@ -187,6 +196,16 @@ export function testBase64Encode_PNG() {
187196
"Base 64 encoded PNG");
188197
}
189198

199+
export function testBase64Encode_PNG_WithQuality() {
200+
const img = imageSource.fromFile(smallImagePath);
201+
let base64String = img.toBase64String("png", 80);
202+
base64String = base64String.substr(0, expectedPngStart.length);
203+
TKUnit.assertEqual(
204+
base64String,
205+
expectedPngStart,
206+
"Base 64 encoded PNG");
207+
}
208+
190209
export function testBase64Encode_JPEG() {
191210
const img = imageSource.fromFile(smallImagePath);
192211

@@ -199,6 +218,18 @@ export function testBase64Encode_JPEG() {
199218
"Base 64 encoded JPEG");
200219
}
201220

221+
export function testBase64Encode_JPEG_With_Quality() {
222+
const img = imageSource.fromFile(smallImagePath);
223+
224+
let base64String = img.toBase64String("jpeg", 80);
225+
base64String = base64String.substr(0, expectedJpegStart.length);
226+
227+
TKUnit.assertEqual(
228+
base64String,
229+
expectedJpegStart,
230+
"Base 64 encoded JPEG");
231+
}
232+
202233
export function testLoadFromBase64Encode_JPEG() {
203234
// >> imagesource-from-base-string
204235
let img: imageSource.ImageSource;

tns-core-modules/image-source/image-source.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,14 @@ export class ImageSource {
9898
* Saves this instance to the specified file, using the provided image format and quality.
9999
* @param path The path of the file on the file system to save to.
100100
* @param format The format (encoding) of the image.
101-
* @param quality Optional parameter, specifying the quality of the encoding. Defaults to the maximum available quality.
101+
* @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.
102102
*/
103103
saveToFile(path: string, format: "png" | "jpeg" | "jpg", quality?: number): boolean;
104104

105105
/**
106106
* Converts the image to base64 encoded string, using the provided image format and quality.
107107
* @param format The format (encoding) of the image.
108-
* @param quality Optional parameter, specifying the quality of the encoding. Defaults to the maximum available quality.
108+
* @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.
109109
*/
110110
toBase64String(format: "png" | "jpeg" | "jpg", quality?: number): string;
111111
}

tns-core-modules/image-source/image-source.ios.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ export class ImageSource implements ImageSourceDefinition {
130130
return false;
131131
}
132132

133+
if (quality) {
134+
quality = quality - 0 / (100 - 0); // Normalize quality on a scale of 0 to 1
135+
}
136+
133137
const data = getImageData(this.ios, format, quality);
134138
if (data) {
135139
return NSFileManager.defaultManager.createFileAtPathContentsAttributes(path, data, null);
@@ -144,6 +148,10 @@ export class ImageSource implements ImageSourceDefinition {
144148
return res;
145149
}
146150

151+
if (quality) {
152+
quality = quality - 0 / (100 - 0); // Normalize quality on a scale of 0 to 1
153+
}
154+
147155
const data = getImageData(this.ios, format, quality);
148156
if (data) {
149157
res = data.base64Encoding();

0 commit comments

Comments
 (0)