Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load a webp image with SDWebImage?

I'm using SDWebImage to load images in my iOS app and I now want to use the webp format.

Here's my first try :

NSURL *url = [NSURL URLWithString:@"http://www.gstatic.com/webp/gallery/2.webp"];

[self.imageView setImageWithURL:url
               placeholderImage:nil
                        options:SDWebImageProgressiveDownload
 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {

     if (error) {

         UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

         [av show];
     }
 }];

It would work perfectly if the image was a jpeg (I tried) but with a webp it doesn't. The first time I call this code the error is :

Downloaded image has 0 pixels

Then the error turns to :

The operation couldn't be completed. (NSURLErrorDomain error -1100.)

What's wrong?

like image 739
Alexis Avatar asked Nov 27 '22 21:11

Alexis


2 Answers

Actually the code above should work but the webp support is disabled by default in the library.

Adding SD_WEBP=1 in the preprocessor macros of the sdwebimage target (in build settings) will enable webp support

like image 199
Alexis Avatar answered Dec 04 '22 09:12

Alexis


Add

pod 'SDWebImage/WebP'

into your podfile and then hit pod install. Cocoapods is gonna take care of the preprocessor flag as well.

From SDWebImage github page

There are 3 subspecs available now: Core, MapKit and WebP (this means you can install only some of the SDWebImage modules. By default, you get just Core, so if you need WebP, you need to specify it).

like image 25
Qamar Suleiman Avatar answered Dec 04 '22 07:12

Qamar Suleiman