How to display base64 image on iPhone in Ionic 4

三世轮回 提交于 2020-04-18 06:12:15

问题


I am using Capacitor Plugins to get the image file (from camera or gallery). PC and Android are working fine, but the code crashes on iPhone.

It opens the galery, I grab the image and it crashes when trying to display

I checked the permissions and they are all set. Why would it crash only on ios? Is it a problem with the string? Security?

HTML:

< ion-img role="button" class="image" [src]="selectedImage" *ngIf="selectedImage" >

TS CODE:

Plugins.Camera.getPhoto({
    quality: 100,
    source: CameraSource.Prompt,
    correctOrientation: true,
    allowEditing: false,
    resultType: CameraResultType.Base64
})
 .then(image => {
      this.selectedImage = image.base64Data; // VAR TO DISPLAY IN HTML
  })

Error log https://i.imgur.com/jrSgGeW.jpg

Edit: Now Im using DomSanitizer and SafeResourceUrl to variable. The error stopped, but the image wont display though


回答1:


Change this line

this.selectedImage = "data:image/jpeg;base64, " + image.base64Data;



回答2:


As you want to display the image and not use the base64 data, then use DataUrl resultType.

For your code sample it should be

Plugins.Camera.getPhoto({
    quality: 100,
    source: CameraSource.Prompt,
    correctOrientation: true,
    allowEditing: false,
    resultType: CameraResultType.DataUrl
})
 .then(image => {
      this.selectedImage = image.dataUrl; // VAR TO DISPLAY IN HTML
 })

But also, as you are using Angular it's recommended to sanitize the data, it should be something like this:

this.selectedImage = this.sanitizer.bypassSecurityTrustResourceUrl(image && (image.dataUrl));

Check a full ionic angular example here https://capacitor.ionicframework.com/docs/guides/ionic-framework-app




回答3:


I had the same issue, but changing the dimensions of the photo in camera options did the trick for me. I was using Cordova, but I'll try my best to convert to capacitor. Hope this helps!

Cordova:

const options: CameraOptions = {
      quality: 75,
      destinationType: this.camera.DestinationType.DATA_URL,
      encodingType: this.camera.EncodingType.JPEG,
      mediaType: this.camera.MediaType.PICTURE,
      saveToPhotoAlbum: true,
      correctOrientation: true,
      targetHeight: 1024,
      targetWidth: 768
    }

Capacitor:

const image = await Camera.getPhoto({ 
       quality: 75,//Test
       source: CameraSource.Prompt,
       correctOrientation: true,
       allowEditing: false,
       resultType: CameraResultType.Base64
       height : 1024,//Test
       width : 768//Test
    }

Capacitor Docs here.




回答4:


From official documentation :

Component:

public getImage(){
const options: CameraOptions = {
  quality: 100,
  destinationType: this.camera.DestinationType.DATA_URL,
  encodingType: this.camera.EncodingType.JPEG,
  mediaType: this.camera.MediaType.PICTURE
}

this.camera.getPicture(options).then((imageData) => {
 // imageData is either a base64 encoded string or a file URI
 // If it's base64 (DATA_URL):
 let base64Image = 'data:image/jpeg;base64,' + imageData;
}, (err) => {
 // Handle error
});
}

Html

<img src={base64Image} />



回答5:


HTML :

<img class="ox-picture" [src]="display(b64)"/>

TS :

constructor(public dms: DomSanitizer) {}

display(b64: string) {
  return this.dms.bypassSecurityTrustUrl("data:image/jpeg;base64," + b64);
}


来源:https://stackoverflow.com/questions/56243999/how-to-display-base64-image-on-iphone-in-ionic-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!