IOS study note 47-asynchronous image loading-egoimageloading

Source: Internet
Author: User

I introduced an open-source image asynchronous loading library in IOS study note 46 -- sdwebimage for asynchronous image loading. Today I will introduce another function-like egoimageloading, the open-source project described in the previous study notes "iOS Study Notes 34-egotableviewpullrefresh implementation pull-down refresh" is the same author. Unlike sdwebimage, egoimageloading implements a custom egoimageview, which is very similar to uiimageview, and also implements automatic cache and manual cache cleaning. Sdwebimage adds a category to the uiimageview. It seems to use the built-in method, while egoimageview is a custom image loading component. This is GitHub Project address: https://github.com/enormego/EGOImageLoading


1. configuration project

Go to the GitHub page and clone the project. There is a demo project in it. You can run it and drag egoimageloading from the downloaded package to your project, and select copy to the project. In this case, you can compile command + B. If the project uses arc, an error is reported. In this case, select the project and select biwould pases, add configuration-fno-objc-arc () to files starting with ego under compile sources ()


Then command + B will see that the compilation is successful, which indicates that the project integration is successful!


Ii. Basic use

It is easy to use. We use this demo to implement a uitableview list of a custom cell, asynchronously load the image display of each cell, and click each cell To Go To The subpage and display a separate image.

First, we need to implement a custom cell that inherits uitableviewcell. the header file is as follows. Declare An egoimageview to display the image, and a uilabel to display the URL of the image:

# Import <uikit/uikit. h> # import "egoimageview. H "@ interface imagecell: uitableviewcell {@ private egoimageview * egoimageview; uilabel * label;} // In-(uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) in indexpath, call to load image display-(void) setimagewithurl :( nsstring *) imageurl; @ end

In the. M file, we initialize these two components and implement setimagewithurl: method:

#import "ImageCell.h"@implementation ImageCell- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];    if (self) {        egoImageView = [[EGOImageView alloc] initWithPlaceholderImage:[UIImage imageNamed:@"placeholder.png"]];        egoImageView.frame = CGRectMake(5, 5, 65, 65);        [self.contentView addSubview:egoImageView];        label = [[UILabel alloc] initWithFrame:CGRectMake(78, 20, 220, 30)];        label.textColor = [UIColor blackColor];        [self.contentView addSubview:label];    }    return self;}-(void)setImageWithURL:(NSString *)imageURL{    [egoImageView setImageURL:[NSURL URLWithString:imageURL]];    label.text = imageURL;}

The palaceholder in the initialization method of egoimageview is the percentage bitmap displayed when loading is not completed or loading fails. Now the custom uitableviewcell is fully implemented, and the page display and functions (key code) are implemented in uitableviewcontroller ):

-(Uitableviewcell *) tableview :( uitableview *) tableview cellforrowatindexpath :( nsindexpath *) indexpath {static nsstring * cellidentifier = @ "cell"; imagecell * cell = (imagecell *) [tableview progress: cellidentifier]; If (cell = nil) {Cell = [[imagecell alloc] initwithstyle: uitableviewcellstyledefault reuseidentifier: cellidentifier];} // imgurls is the nsarray array for storing the image URL [Cell setimagewithurl: [imgurls objectatindex: indexpath. row]; cell. accessorytype = uitableviewcellaccessorydisclosureindicator; return cell ;}

We can see that setimagewithurl in the Custom imagecell is called: The method completes asynchronous loading of images. Isn't it easy? In addition, you can release the cache using egocaceh:

// Clear the cache-(void) javasachebutton {[[egocache currentcache] javasache]; [self. tableview reloaddata];}

Finally, let's take a look at the demo effect:

 


If you need the source code, you can download it here, which provides the complete implementation: 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.