首页 > 解决方案 > 如何知道 ImageSource 是 jpg 还是 png 文件

问题描述

我的应用程序允许用户将个人资料图片从应用程序上传到我的服务器。它适用于 .png 文件,但不适用于 .jpg 文件。

我所做的:

  1. 使用 ImagePicker 允许用户选择图像。
  2. 向用户显示可用的图像。
  3. 当用户点击保存时将所选图像存储在本地
  4. 上传图片到云端

问题出在第 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

标签: angularnativescript

解决方案


查看“saveToFile”方法的文档,我认为它是什么并不重要。您可以尝试选择 jpeg 并查看它是否可以保存为 png(反之亦然)。

(更新)由于源代码中的此注释说 ImageSource 可以是 a ,因此无论输入如何UIImage for iOS and android.graphics.Bitmap for Android,您都应该能够以您喜欢的任何格式保存。您还可以使用iosandroid字段ImageSource来获取有关图像的更多信息(请参阅此链接)


推荐阅读