Latest 0.2.1
License MIT
Platforms osx 10.13, ios 8.0, tvos 10.0
Dependencies SDWebImage/Core
Frameworks Photos

CI Status
Carthage compatible

What’s for

SDWebImagePhotosPlugin is a plugin for SDWebImage framework, which provide the image loading support for Photos Library.

By using this plugin, it allows you to use your familiar View Category method from SDWebImage, to load Photos image with PHAsset or localIdentifier.


  • iOS 8+
  • macOS 10.13+
  • tvOS 10+
  • Xcode 9+



SDWebImagePhotosPlugin is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'SDWebImagePhotosPlugin'


SDWebImagePhotosPlugin is available through Carthage.

github "SDWebImage/SDWebImagePhotosPlugin"


Important! To use Photos Library plugin. Firstly you need to register the photos loader to image manager.

There are two ways to register the photos loader. One for temporarily usage (when providing URL is definitely Photos URL but not HTTP URL), and another for global support (don’t need any check, support both HTTP URL as well as Photos URL).

Use custom manager (temporarily)

You can create custom manager for temporary usage. When you use custom manager, be sure to specify SDWebImageContextCustomManager context option with your custom manager for View Category methods.

  • Objective-C
// Assign loader to custom manager
SDWebImageManager *manager = [[SDWebImageManager alloc] initWithCache:SDImageCache.sharedImageCache loader:SDWebImagePhotosLoader.sharedLoader];
  • Swift
// Assign loader to custom manager
let manager = SDWebImageManager(cache: SDImageCache.shared, loader: SDWebImagePhotosLoader.shared)

Use loaders manager (globally)

You can replace the default manager’s loader implementation using loaders manager to support both HTTP && Photos URL globally. Put these code just at the application launch time (or time just before SDWebImageManager.sharedManager initialized).

  • Objective-C
// Supports HTTP URL as well as Photos URL globally
SDImageLoadersManager.sharedManager.loaders = @[SDWebImageDownloader.sharedDownloader, SDWebImagePhotosLoader.sharedLoader];
// Replace default manager's loader implementation
SDWebImageManager.defaultImageLoader = SDImageLoadersManager.sharedManager;
  • Swift
// Supports HTTP URL as well as Photos URL globally
SDImageLoadersManager.shared.loaders = [SDWebImageDownloader.shared, SDWebImagePhotosLoader.shared]
// Replace default manager's loader implementation
SDWebImageManager.defaultImageLoader = SDImageLoadersManager.shared

Load Images

To start load Photos Library image, use the NSURL+SDWebImagePhotosPlugin to create a Photos URL and call View Category method.

  • Objective-C
// Create with `PHAsset`
PHAsset *asset;
NSURL *photosURL = [NSURL sd_URLWithAsset:asset];
// Create with `localIdentifier`
NSString *identifier;
NSURL *potosURL = [NSURL sd_URLWithAssetLocalIdentifier:identifier];

// Load image (assume using custom manager)
[imageView sd_setImageWithURL:photosURL placeholderImage:nil context:@{SDWebImageContextCustomManager: manager}];
  • Swift
// Create with `PHAsset`
let asset: PHAsset
let photosURL = NSURL.sd_URL(with: asset)
// Create with `localIdentifier`
let identifier: String
let potosURL = NSURL.sd_URL(withAssetLocalIdentifier: identifier)

// Load image (assume using custom manager)
imageView.sd_setImage(with: photosURL, placeholderImage: nil, context: [.customManager: manager])

Animated Images

SDWebImagePhotosPlugin supports GIF images stored in Photos Library as well. Just use the same API as normal images to query the asset. We will query the image data and decode the animated images (compatible with UIImageView as well as SDAnimatedImageView)

Fetch/Request Options

To specify options like PHFetchOptions or PHImageRequestOptions for Photos Library. Either to change the correspond properties in loader, or provide a context options for each image request.

  • Objective-C
// loader-level options
// ignore iCloud Shared Album (`localIdentifier` Photos URL only)
PHFetchOptions *fetchOptions = [PHFetchOptions new];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"sourceType != %d", PHAssetSourceTypeCloudShared];
SDWebImagePhotosLoader.sharedLoader.fetchOptions = fetchOptions;

// request-level options
// allows iCloud Photos Library
PHImageRequestOptions *requestOptions = [PHImageRequestOptions new];
requestOptions.networkAccessAllowed = YES;
[imageView sd_setImageWithURL:photosURL placeholderImage:nil context:@{SDWebImageContextPhotosImageRequestOptions: requestOptions, SDWebImageContextCustomManager: manager}];
  • Swift
// loader-level options
// ignore iCloud Shared Album (`localIdentifier` Photos URL only)
let fetchOptions = PHFetchOptions()
fetchOptions.predicate = NSPredicate(format: "sourceType != %d", PHAssetSourceType.typeCloudShared.rawValue)
SDWebImagePhotosLoader.shared.fetchOptions = fetchOptions

// request-level options
// allows iCloud Photos Library
let requestOptions = PHImageRequestOptions()
requestOptions.networkAccessAllowed = true
imageView.sd_setImage(with: photosURL, placeholderImage: nil, context:[.photosImageRequestOptions: requestOptions, .customManager: manager])


  1. Since Photos Library image is already stored on the device disk. And query speed is fast enough for small resolution image. You can use SDWebImageContextStoreCacheType with SDImageCacheTypeNone to disable cache storage. And use SDWebImageFromLoaderOnly to disable cache query.
  2. If you use PHImageRequestOptionsDeliveryModeOpportunistic (by default) to load the image, PhotosKit will return a degraded thumb image firstly and again with the full pixel image. When the image is degraded, the loader completion block will set finished = NO. But this will not trigger the View Category completion block, only trigger a image refresh (like progressive loading behavior for network image using SDWebImageProgressiveLoad)
  3. By default, we will prefer using Photos requestImageForAsset:targetSize:contentMode:options:resultHandler: API for normal images, using requestImageDataForAsset:options:resultHandler: for animated images like GIF asset. If you need the raw image data for further image processing, you can always pass SDWebImageContextPhotosRequestImageData context option to force using the request data API instead. Note when request data, the targetSize and contentMode options are ignored. If you need smaller image size, consider using Image Transformer feature from SDWebImage 5.0


The Photos taken by iPhone’s Camera, its pixel size may be really large (4K+). So if you want to load large Photos Library assets for rendering, you’d better specify target size with a limited size (like you render imageView’s size).

By default, we query the target size matching the original image pixel size (See: SDWebImagePhotosLoaderPixelSize), which may consume much memory on iOS device.


If you have some issue about usage, SDWebImagePhotosPlugin provide a demo for iOS && macOS platform. To run the demo, clone the repo and run the following command.

cd Example/
pod install
open SDWebImagePhotosPlugin.xcworkspace

After the Xcode project was opened, click Run to build and run the demo.


DreamPiggy, [email protected]


SDWebImagePhotosPlugin is available under the MIT license. See the LICENSE file for more info.

Latest podspec

    "name": "SDWebImagePhotosPlugin",
    "version": "0.2.1",
    "summary": "A SDWebImage plugin to support Photos framework image loading.",
    "description": "This is a SDWebImage loader plugin to support Apple's Photos framework image asset.",
    "homepage": "",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    "authors": {
        "DreamPiggy": "[email protected]"
    "source": {
        "git": "",
        "tag": "0.2.1"
    "platforms": {
        "osx": "10.13",
        "ios": "8.0",
        "tvos": "10.0"
    "source_files": [
    "module_map": "SDWebImagePhotosPlugin/Module/SDWebImagePhotosPlugin.modulemap",
    "frameworks": "Photos",
    "dependencies": {
        "SDWebImage/Core": [
            "~> 5.0"

Pin It on Pinterest

Share This