IOS study notes 46 -- asynchronous image loading sdwebimage

Source: Internet
Author: User

In development, the list loading function is often encountered. Most of the functions include image list loading, but the memory of mobile devices is limited, while loading a large number of images consumes a lot of memory. Today, we will introduce a third-party image asynchronously loaded library sdwebimage. The GitHub address is: repository.

I. Main Functions

  • An uiimageview category adding Web image and Cache Management to the cocoa touch framework
  • An asynchronous image downloader
  • An asynchronous memory + disk image caching with automatic cache expiration handling
  • Animated GIF support
  • Webp format support
  • 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

Including asynchronous download of images, memory and local cache, GIF support, the same URL will not be downloaded repeatedly, and the UI thread will not be blocked.


2. Integration steps

  • Download and unzip the last version of the framework from the download
    Page (download and decompress the framwork package)
  • Right-click on the project navigator and select "add files to" your project ":( right-click the project and select Add Library to the Project)
  • In the dialog, select sdwebimage. Framework :( select sdwebimage. framwork Library)
  • Check the "Copy items into destination group's folder (if needed)" checkbox (select the copy to project check box)
  • Added ImageIO. Framework library support
  • Add linker flag, as shown in figure


Press command + B to compile the project. If the compilation is successful, the integration is successful. If framwork cannot be found, rename the SDWebImage-3.3.framwork in the project directory to sdwebimage. it's okay to compile framwork again. Because I have the latest version 3.3, the path name of the original configuration file may not contain "-3.3", so let's raise this question. After the configuration and compilation are successful, you can start using it:
Iii. Demo implementationFirst, let's take a look at the final implementation, mainly implementing the list loading of uitableview and asynchronous loading of a single image:


Import the following header files that require asynchronous image loading:

# Import <sdwebimage/uiimageview + webcache. h>

In the Protocol method of uitableview, you only need to call the class method of uiimageview to complete asynchronous loading:

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{    static NSString *Identifier = @"UITableViewIdentifier";    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];    if (cell == nil) {        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];    }    NSInteger row = indexPath.row;    cell.textLabel.text = @"My Text";    [cell.imageView setImageWithURL:[arr_imgs objectAtIndex:row]];        return cell;}

Click the item of uitableview To Go To The image loading page and use sdwebimagedownloader to load:

// Download an image [[sdwebimagedownloader shareddownloader] downloadimagewithurl: [nsurl urlwithstring: Self. imgurl] Options: 0 progress: ^ (nsuinteger receivedsize, long expectedsize) {} completed: ^ (uiimage * image, nsdata * data, nserror * error, bool finished) {If (Image & Finished) {imageview. image = image ;}}];

The above only provides key code. For details about the demo, please download the project source code: Download

To join our QQ group or public account, see: Ryan's
Zone public account and QQ Group

At the same time, you are welcome to follow my Sina Weibo chat with me: @ Tang Ren _ Ryan


I think this article is useful to you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.