Skip to content

Asynchronous image downloader with cache support as a UIImageView category

License

Notifications You must be signed in to change notification settings

liuyonghu/SDWebImage

 
 

Repository files navigation

Build Status Pod Version Pod Platform Pod License Dependency Status Reference Status Carthage compatible codecov

This library provides an async image downloader with cache support. For convenience, we added categories for UI elements like UIImageView, UIButton, MKAnnotationView.

Features

  • Categories for UIImageView, UIButton, MKAnnotationView adding web image and cache management
  • An asynchronous image downloader
  • An asynchronous memory + disk image caching with automatic cache expiration handling
  • A background image decompression
  • A guarantee that the same URL won't be downloaded several times
  • A guarantee that bogus URLs won't be retried again and again
  • A guarantee that main thread will never be blocked
  • Performances!
  • Use GCD and ARC

Supported Image Formats

  • Image formats supported by UIImage (JPEG, PNG, ...), including GIF
  • WebP format, including animated WebP (use the WebP subspec)

Requirements

  • iOS 7.0 or later
  • tvOS 9.0 or later
  • watchOS 2.0 or later
  • OS X 10.8 or later
  • Xcode 7.3 or later

Backwards compatibility

Getting Started

Who Uses It

Communication

  • If you need help, use Stack Overflow. (Tag 'sdwebimage')
  • If you'd like to ask a general question, use Stack Overflow.
  • If you found a bug, open an issue.
  • If you have a feature request, open an issue.
  • If you want to contribute, submit a pull request.

How To Use

Objective-C:

#import <SDWebImage/UIImageView+WebCache.h>
...
[imageView sd_setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
             placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
Swift:

import SDWebImage

imageView.sd_setImage(with: URL(string: "http://www.domain.com/path/to/image.jpg"), placeholderImage: UIImage(named: "placeholder.png"))

Animated Images (GIF) support

  • Starting with the 4.0 version, we rely on FLAnimatedImage to take care of our animated images.
  • If you use cocoapods, add pod 'SDWebImage/GIF' to your podfile.
  • To use it, simply make sure you use FLAnimatedImageView instead of UIImageView.
  • Note: there is a backwards compatible feature, so if you are still trying to load a GIF into a UIImageView, it will only show the 1st frame as a static image.
  • Important: FLAnimatedImage only works on the iOS platform. For OS X, use NSImageView with animates set to YES to show the entire animated images and NO to only show the 1st frame. For all the other platforms (tvOS, watchOS) we will fallback to the backwards compatibility feature described above

Common Problems

Using dynamic image size with UITableViewCell

UITableView determines the size of the image by the first image set for a cell. If your remote images don't have the same size as your placeholder image, you may experience strange anamorphic scaling issue. The following article gives a way to workaround this issue:

http://www.wrichards.com/blog/2011/11/sdwebimage-fixed-width-cell-images/

Handle image refresh

SDWebImage does very aggressive caching by default. It ignores all kind of caching control header returned by the HTTP server and cache the returned images with no time restriction. It implies your images URLs are static URLs pointing to images that never change. If the pointed image happen to change, some parts of the URL should change accordingly.

If you don't control the image server you're using, you may not be able to change the URL when its content is updated. This is the case for Facebook avatar URLs for instance. In such case, you may use the SDWebImageRefreshCached flag. This will slightly degrade the performance but will respect the HTTP caching control headers:

[imageView sd_setImageWithURL:[NSURL URLWithString:@"https://graph.facebook.com/olivier.poitrey/picture"]
             placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png"]
                      options:SDWebImageRefreshCached];

Add a progress indicator

Add these before you call sd_setImageWithURL

[imageView sd_setShowActivityIndicatorView:YES];
[imageView sd_setIndicatorStyle:UIActivityIndicatorViewStyleGray];
imageView.sd_setShowActivityIndicatorView(true)
imageView.sd_setIndicatorStyle(.Gray)

Installation

There are three ways to use SDWebImage in your project:

  • using CocoaPods
  • using Carthage
  • by cloning the project into your repository

Installation with CocoaPods

CocoaPods is a dependency manager for Objective-C, which automates and simplifies the process of using 3rd-party libraries in your projects. See the Get Started section for more details.

Podfile

platform :ios, '7.0'
pod 'SDWebImage', '~> 4.0'

If you are using Swift, be sure to add use_frameworks! and set your target to iOS 8+:

platform :ios, '8.0'
use_frameworks!

Subspecs

There are 4 subspecs available now: Core, MapKit, GIF 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).

Podfile example:

pod 'SDWebImage/WebP'

Installation with Carthage (iOS 8+)

Carthage is a lightweight dependency manager for Swift and Objective-C. It leverages CocoaTouch modules and is less invasive than CocoaPods.

To install with carthage, follow the instruction on Carthage

Cartfile

github "rs/SDWebImage"

Installation by cloning the repository

Import headers in your source files

In the source files where you need to use the library, import the header file:

#import <SDWebImage/UIImageView+WebCache.h>

Build Project

At this point your workspace should build without error. If you are having problem, post to the Issue and the community can help you solve it.

Author

Collaborators

Licenses

All source code is licensed under the MIT License.

Architecture

About

Asynchronous image downloader with cache support as a UIImageView category

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 99.1%
  • Ruby 0.9%