YYWebImage Documentation

repository·master·Indexed 26 days ago

https://github.com/ibireme/yywebimage

An asynchronous image loading framework for iOS providing high-performance support for remote and local images. It supports animated formats including WebP, APNG, and GIF, and features progressive image decoding, image transformations, and a manageable memory and disk cache.

Tokens
1.5K
Snippets
5
Records
8
Agent score
36%

What's inside YYWebImage

  1. Install YYWebImage via Carthage

    master

    To install YYWebImage using Carthage:

    1. Add github "ibireme/YYWebImage" to your Cartfile.
    2. Run carthage update --platform ios.
    3. Add the generated framework to your Xcode project.
    4. Import <YYWebImage/YYWebImage.h>.

    Note on WebP support: The Carthage framework does not include the WebP component. To support WebP, use CocoaPods or install the WebP library manually. Use YYImageWebPAvailable() to check availability.

  2. Install YYWebImage via CocoaPods

    master

    To install YYWebImage using CocoaPods:

    1. Update CocoaPods to the latest version.
    2. Add pod 'YYWebImage' to your Podfile.
    3. Run pod install or pod update.
    4. Import <YYWebImage/YYWebImage.h> in your code.

    Note on WebP support: The default pod does not include WebP support. To enable WebP, add pod 'YYImage/WebP' to your Podfile. You can verify installation by calling YYImageWebPAvailable().

  3. Install YYWebImage manually

    master

    To install YYWebImage manually:

    1. Download all files in the YYWebImage subdirectory.
    2. Add the source files to your Xcode project.
    3. Link the following required frameworks:
      • UIKit
      • CoreFoundation
      • QuartzCore
      • AssetsLibrary
      • ImageIO
      • Accelerate
      • MobileCoreServices
      • sqlite3
      • libz
    4. Import YYWebImage.h.

    Note on WebP support: To support WebP, add Vendor/WebP.framework (static library) to your Xcode project. Use YYImageWebPAvailable() to verify.

  4. Load animated images (WebP, APNG, GIF)

    master

    To support animated images like WebP, APNG, or GIF, replace UIImageView with YYAnimatedImageView and set the yy_imageURL property.

    ```objc
    // just replace `UIImageView` with `YYAnimatedImageView` 
    UIImageView *imageView = [YYAnimatedImageView new];
    imageView.yy_imageURL = [NSURL URLWithString:@
  5. Load images progressively

    master

    YYWebImage supports progressive image decoding. You can use yy_setImageWithURL:options: with specific options:

    • YYWebImageOptionProgressive: Enables progressive loading (displaying as it downloads).
    • YYWebImageOptionProgressiveBlur: Enables progressive loading with a blur effect and fade animation.
    • YYWebImageOptionSetImageWithFadeAnimation: Adds a fade-in animation when the image is set.
    // progressive
    [imageView yy_setImageWithURL:url options:YYWebImageOptionProgressive];
    
    // progressive with blur and fade animation
    [imageView yy_setImageWithURL:url options:YYWebImageOptionProgressiveBlur | YYWebImageOptionSetImageWithFadeAnimation];
  6. Load and process images with transform and progress

    master

    Use yy_setImageWithURL:placeholder:options:progress:transform:completion: for advanced workflows. This allows you to:

    1. Download the image.
    2. Track download progress.
    3. Apply transformations (resize, round corners, etc.) via a transform block.
    4. Handle completion and identify the source (e.g., YYWebImageFromDiskCache).
    [imageView yy_setImageWithURL:url
       placeholder:nil
       options:YYWebImageOptionSetImageWithFadeAnimation
       progress:^(NSInteger receivedSize, NSInteger expectedSize) {
           progress = (float)receivedSize / expectedSize;
       }
       transform:^UIImage *(UIImage *image, NSURL *url) {
           image = [image yy_imageByResizeToSize:CGSizeMake(100, 100) contentMode:UIViewContentModeCenter];
           return [image yy_imageByRoundCornerRadius:10];
       }
       completion:^(UIImage *image, NSURL *url, YYWebImageFromType from, YYWebImageStage stage, NSError *error) {
           if (from == YYWebImageFromDiskCache) {
               NSLog(@"load from disk cache");
           }
       }];
  7. Load images from URL

    master

    You can load images from both remote and local URLs by setting the yy_imageURL property on a UIImageView (or other supported views via categories).

    ```objc
    // load from remote url
    imageView.yy_imageURL = [NSURL URLWithString:@
  8. Manage the Image Cache

    master

    Access the cache via [YYWebImageManager sharedManager].cache. You can inspect memory/disk usage or clear the cache.

    Clearing Cache:

    • [cache.memoryCache removeAllObjects] clears the memory cache.
    • [cache.diskCache removeAllObjects] clears the disk cache.
    • [cache.diskCache removeAllObjectsWithProgressBlock:endBlock:] clears the disk cache with progress reporting.
    YYImageCache *cache = [YYWebImageManager sharedManager].cache;
        
    // get cache capacity
    cache.memoryCache.totalCost;
    cache.memoryCache.totalCount;
    cache.diskCache.totalCost;
    cache.diskCache.totalCount;
        
    // clear cache
    [cache.memoryCache removeAllObjects];
    [cache.diskCache removeAllObjects];
        
    // clear disk cache with progress
    [cache.diskCache removeAllObjectsWithProgressBlock:^(int removedCount, int totalCount) {
       // progress
    } endBlock:^(BOOL error) {
       // end
    }];