解决方案更改将包含在即将发布的5.0版中。请检查nativescript-imagepicker团队的评论: https://github.com/NativeScript/nativescript-imagepicker/issues/217
感谢您的时间。
所选图像大小与原始图像不同,如何获取原始文件大小?
您的问题在哪个平台上发生?
请提供您发生问题的以下版本号:
“ nativescript-imagepicker”:“ 6.0.4”
CLI:本机脚本4.2.0
“ tns-core-modules”:“ ^ 4.2.1”,
插件: “ devDependencies”:{
}
是否涉及任何代码? 在这里,我使用@NevinDry's code:
原始图像为2 MB,在'imageSource.saveToFile(path1,“ jpeg”);'之后变为20MB。
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;
}
}
我还尝试了以下解决方案:2MB映像变为6MB。更好,但不正确。
可能还有另一件事,“ imageSource.saveToFile(filePath,fileType);” 原始文件类型为jpg,因此我在这里将fileType保留为jpg。如果使用PNG,则尺寸较大。
pickfile2(){
let context = imagepickerModule.create({
mediaType: 1,//image only
mode: "single"
});
context
.authorize()
.then(function () {
return context.present();
})
.then(selection => {
selection.forEach(selected => {
const ios = selected.ios;
if (ios && ios.mediaType === PHAssetMediaType.Image) {
const opt = PHImageRequestOptions.new();
opt.version = PHImageRequestOptionsVersion.Current;
PHImageManager.defaultManager().requestImageDataForAssetOptionsResultHandler(
ios, opt, (imageData: NSData, dataUTI: string, orientation: UIImageOrientation, info: NSDictionary<any, any>) => {
console.debug(info.objectForKey("PHImageFileURLKey").toString());
let srcfilePath = info.objectForKey("PHImageFileURLKey").toString();
let fileName = this.extractImageName(srcfilePath);
let fileType: "png" | "jpeg" | "jpg" = this.extractImageType(fileName);
console.log("fileName:" + fileName);
console.log("srcfilePath:" + srcfilePath);
//get image instance from data; it doesn't work via 'fromPath'
let imageSource: ImageSource = <ImageSource>fromData(imageData);
let filePath = path.join(this.filePickerPath, fileName);
console.log("filePath:" + filePath);
let saved = imageSource.saveToFile(filePath, fileType);
console.log("saved:" + saved);
if (saved) {
this._checkFileSize(filePath);
}
});
}
});
})
.catch(function (e) {
console.log(e);
});
}
游乐场代码为here
一旦从页面中添加了npm软件包'tns-platform-declarations',页面就会崩溃。因此,如果要运行,请下载它。然后
无论如何,有人对此有想法吗?谢谢。