4

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?

Alexis
  • 16,629
  • 17
  • 62
  • 107

5 Answers5

10

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

Alexis
  • 16,629
  • 17
  • 62
  • 107
  • could u post a source code how to use webp images in ios without taking much time to load them – Mounika Vangala Dec 05 '14 at 13:16
  • Right answer but if u did not install webp submodule then it will give you an error message, so install pod "SDWebImage/WebP" before going to set preprocessor macros. – nabu Feb 26 '18 at 17:08
9

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).

Qamar Suleiman
  • 1,228
  • 2
  • 18
  • 31
6

Swift 5 and Xcode 11

Use pod in podfile

pod 'SDWebImageWebPCoder'

Import this Library

import SDWebImageWebPCoder

Use this code under AppDelegate 'didFinishLaunchingWithOptions'

let WebPCoder = SDImageWebPCoder.shared
SDImageCodersManager.shared.addCoder(WebPCoder)

use this code under ViewController 'viewDidLoad'

@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
    super.viewDidLoad()

    //load from server
    let onlineFileUrl = URL(string: "http://www.gstatic.com/webp/gallery/2.webp")
     //load from project directory
        let loaclFileUrl = Bundle.main.url(forResource: "2", withExtension: "webp")
    DispatchQueue.main.async {
        self.imageView.sd_setImage(with: onlineFileUrl)
    }

}
Zain Ul Abideen
  • 693
  • 10
  • 15
  • it seems that webp file cannot be found through bundle. Any idea? – Stan Nov 21 '20 at 07:56
  • Actually, the problem is solved by manually adding the webp file into bundle. It can be done Project -> Bundle Phase -> copy bundle resouces – Stan Dec 18 '20 at 04:45
4

I have searched many posts that finally lead to here. And here is my solution after asking in github.

https://github.com/rs/SDWebImage/issues/1122

Steps:

1: Clone and add WebP.framework

clone the repository using git clone --recursive https://github.com/rs/SDWebImage.git

add full folder of SDWebImage and WebP.framework into your project.

image

2: in Build Settings -- Preprocessor Macros , add SD_WEBP=1.

image

ps:If using CocoaPods, I don't konw how to add WebP.framework into SDWebImage, so I choose add files into my project.

Community
  • 1
  • 1
iWill
  • 135
  • 1
  • 4
1

Objective-C

Use pod in podfile

pod 'SDWebImageWebPCoder'

Import this Library

#import <SDWebImageWebPCoder.h>

Use this code under AppDelegate 'didFinishLaunchingWithOptions'

SDImageWebPCoder *WebPCoder = [SDImageWebPCoder sharedCoder];
[[SDImageCodersManager  sharedManager] addCoder:WebPCoder];

to download image

[self.yourImage sd_setImageWithURL:[NSURL URLWithString:@"your_url_webp" placeholderImage:[UIImage imageNamed:@"placeholder"]];