IOS : Selected image size is different from the original image
Selected image size is different from the original image, How to get the original file size ?
Which platform(s) does your issue occur on?
iOS emulator or device. What type of device? emulator IOS 11.4 on iphone 8 Plus real device IOS 11.4 on IPad air
Please, provide the following version numbers that your issue occurs with:
"nativescript-imagepicker": "6.0.4"
CLI: Nativescript 4.2.0 "tns-core-modules": "^4.2.1", "tns-platform-declarations": "^3.3.0"
Plugin(s):
"devDependencies": { "babel-traverse": "6.26.0", "babel-types": "6.26.0", "babylon": "6.18.0", "lazy": "1.0.11", "nativescript-dev-typescript": "0.7.2", "typescript": "~2.7.2" }
Is there any code involved?
Here I use @NevinDry's code:
the original image is 2 MB, and it became 40MB after 'imageSource.saveToFile(path1, "jpeg");'
pickFile() {
let context = imagepickerModule.create({
mediaType: 1,//image only
mode: "single"
});
context
.authorize()
.then(function () {
return context.present();
})
.then(selection => {
selection.forEach(selected => {
let file;
fromAsset(selected).then((imageSource) => {
const folder = knownFolders.documents().path;
const fileName = "test.jpg";
const path1 = path.join(folder, fileName);
const saved = imageSource.saveToFile(path1, "jpeg");
console.log("saved:" + saved);
if (saved) {
console.log("Image saved successfully!");
file = File.fromPath(path1);
console.log("path1:" + path1);
this._checkFileSize(path1);
}
})
});
})
.catch(function (e) {
console.log(e);
});
}
private _checkFileSize(fileUri: string): boolean {
if (isIOS) {
var defManager = NSFileManager.defaultManager;
console.debug("the file path:" + fileUri);
var fileAttributes = defManager.attributesOfItemAtPathError(fileUri);
console.debug(defManager);
var fileSizeNumber = fileAttributes.objectForKey(NSFileSize);
console.debug(fileSizeNumber);
var fileSizeNumberB = fileSizeNumber / 1000000.0;
console.debug("file size in Megabytes");
console.debug(fileSizeNumberB);
return true;
}
}
Does anyone have any idea on it ? Thanks.
Hi @anorthwolf,
This issue seems to be related to the one reported here about the imageSource. This should be resolved in the upcoming release 5.0 with the new property autoScaleFactor, which will allow you to disable the auto scale of the images and should solve the image size issue as well. Check out the PR here.
Thanks @tsonevn . Looking forward to it.