Front-end time to learn more about the AFNetworking library, so check out the official API. Think about taking notes since you read them. Since you took notes, why not try to translate them? Then came the previous article “AFNetworking Instruction manual”, and when I had some time to spare, I wanted to read another API document. The SDWebImage Specification article was selected. This was selected because it was used a lot and commented that the code style was good. It is neck and neck with the AFNetworking manual.

Recommend next own article ha. Interested can have a look, of course I English newbie, translation wrong place, please give more advice.

The classes, categories and protocols involved need to be clicked to jump to the new page for viewing.

This library provides an asynchronous image loader with caching capabilities. For convenience, we added categories for UI elements such as UIImageView, UIButton, and MKAnnotationView.

features

  • Create classes for UIImageView, UIButton, MKAnnotationView to add web images and cache management.
  • An asynchronous picture loader.
  • An asynchronous memory + disk image cache with automatic cache expiration processing.
  • A background image extractor.
  • Make sure the same URL is not downloaded multiple times.
  • Make sure that incorrect urls are not retried again and again.
  • Make sure the main thread is not blocked.
  • Performance optimization.
  • Use GCD and ARC.

Support for image formatting

  • Image formats supported by UIImage, such as JPEG, PNG, etc., including GIF.
  • WebP format, including dynamic WebP (using WebP subspaces).

requirements

  • IOS 7.0 or later
  • TvOS 9.0 or later
  • WatchOS 2.0 or later
  • OS X10.8 or later
  • Xcode7.3 or above

Backward compatibility

  • IOS5 and 6, use 3. Think of any version between 3.7.6.
  • If the version is later than iOS5.0, use the latest version 2.0.

start

  • Read the Readme Document.
  • Read the “How to Use” section.
  • Read the @cocoadocs document.
  • Read “Why SDWebImage is Better than X”.
  • It’s easier to download the project from Github or try the SDWebImage sample using CocoaPods.
  • View Installation Procedure.
  • Read “SDWebImage4.0 migration guide” to learn about 3.x to 4.x changes.

Who is using

  • Find out “who is using SDWebImage” and add your app to the list.

communication

  • If you need help, use Stack Overflow. (‘ sdwebiamge ‘tag)
  • If you want to ask a general question, use Stack Overflow.
  • If you find a bug, please open a new question.
  • If you have a feature request, please open a new question.
  • If you want to contribute, please 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"]].Copy the code

Swift

import SDWebImage

imageView.sd_setImage(with: URL(string: "http://www.domain.com/path/to/image.jpg"), placeholderImage: UIImage(named: "placeholder.png"))
Copy the code
  • For more information on how to use this library and clear examples, see “How to Use it in detail”.

Animated pictures (GIF) support

  • As of version 4.0, we rely on FLAnimatedImage to process our animated images.
  • If you use Cocoapods, add ‘SDWebImage/GIF’ to your podfile.
  • To use it, just make sure you use FLAnimatedImageView instead of UIImageView.
  • ** Note: ** has a backward compatibility feature, so if you still try to load giFs into UIImageView, it will only show the first frame of the static image.
  • ** Important: **FLAnimatedImage only works on iOS. On OX X, use NSImageView and set animates to YES to display the entire animation picture, set NO to display only the first frame. For all other platforms (tvOS, watchOS), we’ll fall back on the backward compatibility features described above.

Q&A

UITableViewCell uses dynamic image sizes

UITableView sets up a cell by detecting the size of the first image. If your remote image is not the same size as your default image, you may encounter strange image distortion problems. The following article provides a solution to the problem of thought: [www.wrichards.com/blog/2011/1]… (www.wrichards.com/blog/2011/1)… .

Handle image refresh

SDWebImage has a very strong caching mechanism by default. It will ignore all cache types that control headers in HTTP server returns, and will cache the returned images without limit. It means that your image URLS are static URLS that point to images that never change. If the pointing image is changed, the URL part should be changed accordingly. If you do not control the image server you are using, you cannot change the URL of an image when the image content changes. Take Facebook’s avatar URLS for example. In this case, you can use the SDWebImageRefreshCached tag. This will reduce performance slightly but will comply with the HTTP cache control header:

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

Add a progress indicator

Add the following code before you call sd_setImageWithURL

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

The installation

There are three ways to use SDWebImage in your project:

  • Using CocoaPods
  • The use of Carthage
  • Add to your warehouse via clone project

PODFILE

platfrom :ios, '7.0'
pod 'SDWebImage'.'~ > 4.0'
Copy the code

If you are using Swift, make sure you add use_frameworks! And set the target version to iOS 8+ :

platform: ios, '8.0'
use_frameworks!
Copy the code

SUBSPECS now has 4 valid submodules: Core, MapKit, GIF, and WebP (this means you can install only the specified modules for SDWebImage. By default, you only get Core modules, so if you need WebP, you need to specify it). Podfile sample:

pod 'SDWebImage/WebP'
Copy the code

CARTFILE

github "rs/SDWebImage"
Copy the code

Install via clone repository

  • For details, see Manual Installation.

Introduce header files in your source files

In the source files you need to use this library, introduce headers:

##import <SDWebImage/UIImageView+WebCache.h>
Copy the code

Compile the project

At this point, your workspace should compile without errors. If you have a problem, send in your problem and the community can help you solve it.

The author

  • Olivier Poitrey

collaborators

  • Konstantinos K.
  • Bogdan Poplauschi
  • Chester Liu

The license

All source code is licensed under the MIT license.

architecture

Class description

  • SDImageCache
  • SDImageCacheConfig
  • SDWebImageDownloadToken
  • SDWebImageDownloader
  • SDWebImageDownloaderOperation
  • SDWebImageManager
  • SDWebImagePrefetcher

Agreement that

  • SDWebImageDownloaderOperationInterface
  • SDWebImageManagerDelegate
  • SDWebImageOperation
  • SDWebImagePrefetcherDelegate

Classification of instructions

  • FLAnimatedImageView(WebCache)
  • MKAnnotationView(WebCache)
  • NSData(ImageContentType)
  • NSImage(WebCache)
  • UIButton(WebCache)
  • UIImage(ForceDecode)
  • UIImage(GIF)
  • UIImage(MultiFormat)
  • UIImage(WebP)
  • UIImageView(HighlightedWebCache)
  • UIImageView(Webcache)
  • UIView(WebCache)
  • UIView(WebCacheOperation)

Constants that

  • SDImageCacheType
  • SDImageFormat
  • SDWebImageDownloaderExecutionOrder
  • SDWebImageDownloaderOptions
  • SDWebImageOptions

// END pull down the reading volume, no dry goods. Next I want to learn the code of YY God, and I hope I can stick to it. Come on!!