Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config to set image as blob #352

Closed
superainovacoes opened this issue Jun 14, 2018 · 1 comment
Closed

Add config to set image as blob #352

superainovacoes opened this issue Jun 14, 2018 · 1 comment

Comments

@superainovacoes
Copy link

superainovacoes commented Jun 14, 2018

Example of loading it as blob

document.write("<img id="imageblob"/>")
function response(e) {
var urlCreator = window.URL || window.webkitURL;
var imageUrl = urlCreator.createObjectURL(this.response);
document.querySelector("#imageblob").src = imageUrl;
}
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://demo.cloudimg.io/width/616/n/https://gallery.airstore.io/birds.jpg");
xhr.responseType = "blob";
xhr.onload = response;
xhr.send();

@tjoskar
Copy link
Owner

tjoskar commented Jun 16, 2018

Yes, I want to add the ability to add custom load- and set-functions (see #304).

But I guess you should be able to load a blob as it is:

@Component({
    selector: 'my-image',
    template: `
        <img
          [defaultImage]="defaultImage"
          [errorImage]="errorImage"
          [lazyLoad]="image"
          (onLoad)="onLoad($event)">
    `
})
export class MyImageComponent {
    errorImage = 'https://i.imgur.com/XkU4Ajf.png';
    defaultImage = 'https://www.placecage.com/1000/1000';
    image = this.defaultImage;
    loaded = false

    onLoad(success: boolean) {
      if (!success || loaded) {
        return
      }
      this.loaded = true
       fetch('https://demo.cloudimg.io/width/616/n/https://gallery.airstore.io/birds.jpg')
         .then(response => {
           if(response.ok) {
             return response.blob();
           }
           throw new Error('Network response was not ok.');
         }).then(myBlob => { 
           const objectURL = URL.createObjectURL(myBlob); 
           this.image = objectURL; 
         }).catch(error => {
           console.error(error);
           this.image = errorImage; 
         });
    }
}

This code will:

  1. Load the default image and put it into the DOM
  2. When the image is in viewport it will once again load the default image but since it is already loaded it will emit the onLoad-event
  3. The onLoad function will be trigged and fetch the "real" image
  4. When the new image is loaded, it will assign the url to image and angular will trigger a rerender
  5. ng-lazyload-image will now try to load the "real" image but since it is alreday loaded, it will just replace the src attrebute.

However, this is not a good solution! This is just a hack untill #304 is resolved.

@tjoskar tjoskar closed this as completed Aug 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants