angular - 如何知道 ImageSource 是 jpg 还是 png 文件
问题描述
我的应用程序允许用户将个人资料图片从应用程序上传到我的服务器。它适用于 .png 文件,但不适用于 .jpg 文件。
我所做的:
- 使用 ImagePicker 允许用户选择图像。
- 向用户显示可用的图像。
- 当用户点击保存时将所选图像存储在本地
- 上传图片到云端
问题出在第 3 步,我不知道如何知道文件应该保存为 png 还是 jpg。
startSelection(context: ImagePicker) {
context
.authorize()
.then(() => {
return context.present();
})
.then(selection => {
selection.forEach(selectedItem => {
ImageSource.fromAsset(selectedItem).then(imageSource => {
this.imageUrls.push(imageSource);
this.selectedUrlIndex = this.imageUrls.length - 1;
});
});
})
.catch(err => {
// ...
});
}
onSave(): void {
const folder = fs.knownFolders.documents();
const path = fs.path.join(folder.path, 'avatar.png');
const saved = this.getSelectedAvatar().saveToFile(path, 'png');
this.userService
.sendImages(path)
.then(() => {
// ...
});
}
我的问题是在这一行this.getSelectedAvatar().saveToFile(path, 'png');
,getSelectedAvatar 返回一个ImageSource
解决方案
查看“saveToFile”方法的文档,我认为它是什么并不重要。您可以尝试选择 jpeg 并查看它是否可以保存为 png(反之亦然)。
(更新)由于源代码中的此注释说 ImageSource 可以是 a ,因此无论输入如何UIImage for iOS and android.graphics.Bitmap for Android
,您都应该能够以您喜欢的任何格式保存。您还可以使用ios
或android
字段ImageSource
来获取有关图像的更多信息(请参阅此链接)。
推荐阅读
- assembly - Qemu 和原始二进制文件
- node.js - 过滤 AWS describeInstances 请求参数
- amazon-web-services - AWS Glue Crawler 可以处理同一文件夹中的不同文件类型吗?
- sql-server - Docker 检查点因 SQL Server 容器而失败
- javascript - 请求未返回正确值的 Javascript 函数
- javascript - 无法启动 forge-cli
- swift - 更新的核心数据模型属性不可访问?
- javascript - 如何在 CKEditor5 中创建 Matcher 实例?
- windows - 使用 PowerShell 按日期过滤文件夹
- ios - 有人可以解释为什么我的 CollectionView 单元格只出现在这种情况下吗?