PINRemoteImage

repository·master·Indexed 26 days ago

https://github.com/pinterest/pinremoteimage

A fast, non-deadlocking parallel image downloader and cache for iOS. It supports PNG, JPG, WebP, and GIFs, featuring progressive JPEG rendering with blur, custom image processing, and high-resolution image support. The library integrates with PINCache and PINOperation for efficient memory and disk caching.

Tokens
1.9K
Snippets
6
Records
19
Agent score
87%

What's inside PINRemoteImage

  1. Install PINCache

    master

    You can install PINCache using several methods:

    CocoaPods

    Add PINCache to your Podfile and run pod install.

    Carthage

    Add pinterest/PINCache to your Cartfile and run carthage update --platform ios. Follow the standard Carthage instructions to embed the framework.

    Git Submodule

    git submodule add https://github.com/pinterest/PINCache.git
    git submodule update --init

    Manual Installation

    Download the latest tag from the GitHub repository and drag the PINCache folder into your Xcode project.

    git submodule add https://github.com/pinterest/PINCache.git
    git submodule update --init
  2. Install PINRemoteImage manually or via Git Submodule

    master

    Manual Installation

    1. Download the latest tag from the GitHub releases page.
    2. Drag the Pod/Classes folder into your Xcode project.
    3. Important: You must manually link against PINCache.

    Git Submodule

    Add the repository as a submodule and initialize it:

    git submodule add https://github.com/pinterest/PINRemoteImage.git
    git submodule update --init

    After adding the submodule, follow the manual installation steps above.

  3. Support high resolution images

    master

    PINRemoteImage supports high-resolution images in two ways:

    1. Automatic Suffix Handling: If the URL contains an _2x. or _3x. postfix, the manager automatically handles it and returns the image at the correct scale.
    2. Manual Completion Handler: If suffixes aren't used, you can manually apply the screen scale in the completion handler.

    Objective-C Manual Scale Example

    [self.imageView pin_setImageFromURL:url completion:^(PINRemoteImageManagerResult * _Nonnull result) {
      CGFloat scale = UIScreen.mainScreen.scale;
      if (scale > 1.0) {
        UIImage *image = result.image;
        weakImageView.image = [UIImage imageWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation];
      }
    }];
  4. Configure cache limits

    master

    You can customize the memory and disk cache limits via the PINCache instance held by PINRemoteImageManager.

    • Memory Cache: Cost is based on pixel count.
    • Disk Cache: Limits can be set by bytes or age.

    Example Configuration

    PINCache *cache = (PINCache *)[[PINRemoteImageManager sharedImageManager] cache];
    
    // Set memory cost limit
    [[cache memoryCache] setCostLimit:600 * [[UIScreen mainScreen] scale] * 600 * [[UIScreen mainScreen] scale] * 100];
    
    // Set disk byte limit (~50 MB)
    [[cache diskCache] setByteLimit:50 * 1024 * 1024];
    
    // Set disk age limit (30 days)
    [[cache diskCache] setAgeLimit:60 * 60 * 24 * 30];
  5. Download and process an image with a custom processor

    master

    You can apply custom transformations (like rounding corners) to an image during the download process using a processor block. Use a processorKey to ensure the processed version is cached separately from the original.

    Swift Example (Rounding Corners)

    let imageView = UIImageView()
    imageView.pin_setImage(from: URL(string: "https://i.pinimg.com/736x/5b/c6/c5/5bc6c5387ff6f104fd642f2b375efba3.jpg")!, processorKey: "rounded") { (result, unsafePointer) -> UIImage? in
        guard let image = result.image else { return nil }
        // ... implementation of image processing ...
        return processedImage
    }