| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent e1a1d64 commit 319c153
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -54,6 +54,15 @@ export function testSaveToFile() { | |||
| 54 | 54 | TKUnit.assert(fs.File.exists(path), "Image not saved to file"); | |
| 55 | 55 | } | |
| 56 | 56 | ||
| 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 | + | ||
| 57 | 66 | export function testFromFile() { | |
| 58 | 67 | // >> imagesource-load-local | |
| 59 | 68 | const folder = fs.knownFolders.documents(); | |
@@ -187,6 +196,16 @@ export function testBase64Encode_PNG() { | |||
| 187 | 196 | "Base 64 encoded PNG"); | |
| 188 | 197 | } | |
| 189 | 198 | ||
| 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 | + | ||
| 190 | 209 | export function testBase64Encode_JPEG() { | |
| 191 | 210 | const img = imageSource.fromFile(smallImagePath); | |
| 192 | 211 | ||
@@ -199,6 +218,18 @@ export function testBase64Encode_JPEG() { | |||
| 199 | 218 | "Base 64 encoded JPEG"); | |
| 200 | 219 | } | |
| 201 | 220 | ||
| 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 | + | ||
| 202 | 233 | export function testLoadFromBase64Encode_JPEG() { | |
| 203 | 234 | // >> imagesource-from-base-string | |
| 204 | 235 | let img: imageSource.ImageSource; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -98,14 +98,14 @@ export class ImageSource { | |||
| 98 | 98 | * Saves this instance to the specified file, using the provided image format and quality. | |
| 99 | 99 | * @param path The path of the file on the file system to save to. | |
| 100 | 100 | * @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. | ||
| 102 | 102 | */ | |
| 103 | 103 | saveToFile(path: string, format: "png" | "jpeg" | "jpg", quality?: number): boolean; | |
| 104 | 104 | ||
| 105 | 105 | /** | |
| 106 | 106 | * Converts the image to base64 encoded string, using the provided image format and quality. | |
| 107 | 107 | * @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. | ||
| 109 | 109 | */ | |
| 110 | 110 | toBase64String(format: "png" | "jpeg" | "jpg", quality?: number): string; | |
| 111 | 111 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -130,6 +130,10 @@ export class ImageSource implements ImageSourceDefinition { | |||
| 130 | 130 | return false; | |
| 131 | 131 | } | |
| 132 | 132 | ||
| 133 | + if (quality) { | ||
| 134 | + quality = quality - 0 / (100 - 0); // Normalize quality on a scale of 0 to 1 | ||
| 135 | + } | ||
| 136 | + | ||
| 133 | 137 | const data = getImageData(this.ios, format, quality); | |
| 134 | 138 | if (data) { | |
| 135 | 139 | return NSFileManager.defaultManager.createFileAtPathContentsAttributes(path, data, null); | |
@@ -144,6 +148,10 @@ export class ImageSource implements ImageSourceDefinition { | |||
| 144 | 148 | return res; | |
| 145 | 149 | } | |
| 146 | 150 | ||
| 151 | + if (quality) { | ||
| 152 | + quality = quality - 0 / (100 - 0); // Normalize quality on a scale of 0 to 1 | ||
| 153 | + } | ||
| 154 | + | ||
| 147 | 155 | const data = getImageData(this.ios, format, quality); | |
| 148 | 156 | if (data) { | |
| 149 | 157 | res = data.base64Encoding(); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments