Nativescript ImageSource toBase64String输出null

时间:2018-08-28 20:54:43

标签: angular nativescript

在尝试将图像路径转换为base64字符串时,使用以下功能,我在IOS模拟器上遇到错误:

imageUploaded(imagePath): void {
  console.log(imagePath);
  this.newImage = new ImageSource();
  this.newImage.fromFile(imagePath)
    .then((success) => {
      if (success) {
        const base64 = this.newImage.toBase64String("jpeg", 100);
        console.log(base64);
      }
    });
}

两个控制台日志的输出是:

  

file:///Users/coffee-pot/Library/Developer/CoreSimulator/Devices/F0AE2C35-079B-4D8B-AE57-24A3A9A7F931/data/Media/DCIM/100APPLE/IMG_0006.PNG

  

我已经被这个问题困扰了很长时间,无法弄清楚:/

预先感谢您的提示。

修改 以下功能是上传图像并发出带有文件路径的事件的功能

uploadImage() {
const milliseconds = new Date().getTime();
this.context.authorize()
.then(() => {
  return this.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 >) => {
            const folder = fileSystemModule.knownFolders.documents();
            const path = fileSystemModule.path.join(folder.path, milliseconds + ".png");
            this.newImage = info.objectForKey("PHImageFileURLKey").toString();
            this.imagePath.emit(this.newImage);
          });
        }
      });
    });
  }

0 个答案:

没有答案