diff --git a/Example/SDWebImageWebPCoderExample/Base.lproj/Main.storyboard b/Example/SDWebImageWebPCoderExample/Base.lproj/Main.storyboard index a9e7132..63ba886 100644 --- a/Example/SDWebImageWebPCoderExample/Base.lproj/Main.storyboard +++ b/Example/SDWebImageWebPCoderExample/Base.lproj/Main.storyboard @@ -1,11 +1,11 @@ - + - + @@ -17,23 +17,9 @@ - - - - - - - - - - - - - - diff --git a/Example/SDWebImageWebPCoderExample/ViewController.m b/Example/SDWebImageWebPCoderExample/ViewController.m index dbf57d7..f2dbccb 100644 --- a/Example/SDWebImageWebPCoderExample/ViewController.m +++ b/Example/SDWebImageWebPCoderExample/ViewController.m @@ -11,7 +11,8 @@ #import @interface ViewController () -@property (weak, nonatomic) IBOutlet UIImageView *imageView; +@property (nonatomic, strong) UIImageView *imageView1; +@property (nonatomic, strong) SDAnimatedImageView *imageView2; @end @@ -23,9 +24,40 @@ [[SDImageCodersManager sharedManager] addCoder:[SDImageWebPCoder sharedCoder]]; - [self.imageView sd_setImageWithURL:[NSURL URLWithString:@"http://littlesvr.ca/apng/images/SteamEngine.webp"]]; + self.imageView1 = [UIImageView new]; + self.imageView1.contentMode = UIViewContentModeScaleAspectFit; + [self.view addSubview:self.imageView1]; + + self.imageView2 = [SDAnimatedImageView new]; + self.imageView2.contentMode = UIViewContentModeScaleAspectFit; + [self.view addSubview:self.imageView2]; + + NSURL *staticWebPURL = [NSURL URLWithString:@"https://www.gstatic.com/webp/gallery/2.webp"]; + NSURL *animatedWebPURL = [NSURL URLWithString:@"http://littlesvr.ca/apng/images/world-cup-2014-42.webp"]; + + [self.imageView1 sd_setImageWithURL:staticWebPURL completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { + if (image) { + NSLog(@"%@", @"Static WebP load success"); + } + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + NSData *webpData = [image sd_imageDataAsFormat:SDImageFormatWebP]; + if (webpData) { + NSLog(@"%@", @"WebP encoding success"); + } + }); + }]; + [self.imageView2 sd_setImageWithURL:animatedWebPURL completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) { + if (image) { + NSLog(@"%@", @"Animated WebP load success"); + } + }]; } +- (void)viewWillLayoutSubviews { + [super viewWillLayoutSubviews]; + self.imageView1.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height / 2); + self.imageView2.frame = CGRectMake(0, self.view.bounds.size.height / 2, self.view.bounds.size.width, self.view.bounds.size.height / 2); +} - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; diff --git a/Example/Screenshot/WebPDemo.png b/Example/Screenshot/WebPDemo.png new file mode 100644 index 0000000..8e827cd Binary files /dev/null and b/Example/Screenshot/WebPDemo.png differ diff --git a/README.md b/README.md index cdcfc0d..15f2dcb 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,9 @@ [![Platform](https://img.shields.io/cocoapods/p/SDWebImageWebPCoder.svg?style=flat)](http://cocoapods.org/pods/SDWebImageWebPCoder) [![Carthage compatible](https://img.shields.io/badge/Carthage-compatible-4BC51D.svg?style=flat)](https://github.com/SDWebImage/SDWebImageWebPCoder) -Starting with the SDWebImage 5.0 version, we moved the [libwebp](https://github.com/webmproject/libwebp) support code from the Core Repo to this stand-alone repo. +Starting with the SDWebImage 5.0 version, we moved the WebP support code and [libwebp](https://github.com/webmproject/libwebp) from the Core Repo to this stand-alone repo. + +SDWebImageWebPCoder supports both WebP decoding and encoding, for Static WebP or Animated WebP as well. ## Requirements @@ -35,13 +37,38 @@ github "SDWebImage/SDWebImageWebPCoder" ## Usage -```objective-c -SDImageWebPCoder *webPCoder = [SDImageWebPCoder sharedCoder]; -[[SDWebImageCodersManager sharedInstance] addCoder:webPCoder]; ++ Objective-C +```objective-c +// Add coder +SDImageWebPCoder *webPCoder = [SDImageWebPCoder sharedCoder]; +[[SDImageCodersManager sharedManager] addCoder:webPCoder]; + +// WebP image loading UIImageView *imageView; -NSURL *WebPURL = ...; -[imageView sd_setImageWithURL:WebPURL]; +NSURL *webpURL; +[imageView sd_setImageWithURL:webpURL]; + +// WebP image encoding +UIImage *image; +NSData *webpData = [UIImage sd_imageDataAsFormat:SDImageFormatWebP]; +``` + ++ Swift + +```swift +// Add coder +let WebPCoder = SDImageWebPCoder.shared +SDImageCodersManager.shared.addCoder(WebPCoder) + +// WebP online image loading +let webpURL: URL +let imageView: UIImageView +imageView.sd_setImage(with: webpURL) + +// WebP image encoding +let image: UIImage +let webpData = image.sd_imageData(asFormat: .WebP) ``` ## Example @@ -50,10 +77,19 @@ To run the example project, clone the repo, and run `pod install` from the Examp This is a demo to show how to use `WebP` and animated `WebP` images via `SDWebImage`. +## Screenshot + + + +These WebP images are from [WebP Gallery](https://developers.google.com/speed/webp/gallery1) and [GIF vs APNG vs WebP](http://littlesvr.ca/apng/gif_apng_webp.html) + ## Author [Bogdan Poplauschi](https://github.com/bpoplauschi) +[DreamPiggy](https://github.com/dreampiggy) ## License SDWebImageWebPCoder is available under the MIT license. See [the LICENSE file](https://github.com/SDWebImage/SDWebImageWebPCoder/blob/master/LICENSE) for more info. + +