javascript - Nativescript:用黑色空间代替照片缩略图

标签 javascript image base64 nativescript

请帮忙,我正在制作用相机拍摄的照片的缩略图,并将其编码为 base64。

Javascript:

  class Entry {

  get thumbnail() {
        return (this._thumbnail || null);
  }

  set thumbnail(value) {
        let scaledImage = false;
        if (value) {
            if (value.android) {
                const aspectSafeSize = common.getAspectSafeDimensions(value.width, value.height, config.thumbWidth, config.thumbHeight);
                scaledImage = android.graphics.Bitmap.createScaledBitmap(value.android, aspectSafeSize.width, aspectSafeSize.height, true);
            }
            if (value.ios) {
                const cgSize = CGSizeMake(config.thumbWidth, config.thumbHeight);
                UIGraphicsBeginImageContextWithOptions(cgSize, false, 0.0);
                value.ios.drawInRect(CGRectMake(0, 0, config.thumbWidth, config.thumbHeight));
                scaledImage = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
            }
        }
        if (scaledImage) {
            const scaledImageSource = new ImageSourceModule.ImageSource();
            scaledImageSource.setNativeSource(scaledImage);
            const t = 'data:image/png;base64,' + scaledImageSource.toBase64String('png',100);
            this._thumbnail = t;
        }
    }
}

在 XML 中:

<Image top="0" left="0" src="{{ thumbnail }}" stretch="aspectFill" visibility="{{ thumbnail ? 'visible' : 'collapsed' }}"/>

但只显示黑色空间:screenshot 。 而且,如果获取编码数据并尝试对其进行解码,我们会得到一张正常的图片

最佳答案

如果您为任何父元素设置了 CSS 属性颜色: this is documented bug在 NativeScript 2.3.0 中,并计划在下一版本 (2.4.0) 中修复。如果您现在想使用下一个版本,可以按照以下步骤进行:

tns plugin remove tns-core-modules
tns plugin add tns-core-modules@next

现在您将拥有已应用修复程序的最新模块。

关于javascript - Nativescript:用黑色空间代替照片缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39700037/

相关文章:

javascript - 我希望我的元素淡入淡出,然后页面关闭并加载另一个页面

html - 将多张不同尺寸的图片排成固定宽度并调整高度

javascript - 我如何在点击 'refresh' 时完全重新加载我的 Sencha Touch 应用程序?

javascript - 按列表从字符串中删除所有 html 标记,第一个除外

javascript - jScrollPane Vertical Tabs - 如何模拟点击链接/标签?

web-services - 使用Delphi 2007将Base64字符串作为二进制文件保存到磁盘

python - 使用Python将base64编码的图像推送到Google Contacts

Android Gallery 放大/缩小

php - 使用输入文件类型编辑图像

javascript - PHP base64_decode 未将 base64 字符串转换为真正可用的图像文件