Asynchronous image loading for iOS development

Source: Internet
Author: User
Original method: asyncimageview. h:

#import <UIKit/UIKit.h>

@interface AsyncImageView : UIView

{

    NSURLConnection* connection;

    NSMutableData* data;

}

- (void)loadImageFromURL:(NSURL*)url;

@end

Asyncimageview. M:

#import "AsyncImageView.h"

@implementation AsyncImageView

  

- (id)initWithFrame:(CGRect)frame

{

    self = [super initWithFrame:frame];

    if(self) {

        // Initialization code

    }

    returnself;

}

  

- (void)loadImageFromURL:(NSURL*)url {

    if(connection!=nil) { [connection release]; }

    if(data!=nil) { [data release]; }

    NSURLRequest* request = [NSURLRequest requestWithURL:url

                                             cachePolicy:NSURLRequestUseProtocolCachePolicy

                                         timeoutInterval:60.0];

    connection = [[NSURLConnection alloc]

                  initWithRequest:request delegate:self];

}

  

- (void)connection:(NSURLConnection *)theConnection

    didReceiveData:(NSData *)incrementalData {

    if(data==nil) {

        data =

        [[NSMutableData alloc] initWithCapacity:2048];

    }

    [data appendData:incrementalData];

}

  

- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {

  

    [connection release];

    connection=nil;

  

    if([[self subviews] count] > 0) {

        [[[self subviews] objectAtIndex:0] removeFromSuperview];

    }

  

    UIImageView *imageView = [[[UIImageView alloc] initWithImage:[UIImage imageWithData:data]] autorelease];

  

    imageView.contentMode = UIViewContentModeScaleAspectFit;

    imageView.autoresizingMask = ( UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight );

  

    [self addSubview:imageView];

    imageView.frame = self.bounds;

    [imageView setNeedsLayout];

    [self setNeedsLayout];

    [data release];

    data=nil;

}

  

- (UIImage*) image {

    UIImageView* iv = [[self subviews] objectAtIndex:0];

    return[iv image];

}

  

- (void)dealloc {

    [connection cancel];

    [connection release];

    [data release];

    [super dealloc];

}

@end

Usage:

AsyncImageView *asyncImage = [[AsyncImageView alloc] init];

asyncImage.frame = CGRectMake(100, point_y, 95, 95)

// Rounded corner

[asyncImage.layer setMasksToBounds:YES];

[asyncImage.layer setCornerRadius:15];

[asyncImage loadImageFromURL:[NSURL URLWithString:@"www.istar.name/...."]];

However, it is too convenient to find a good thing, sdwebimage.
Home: https://github.com/rs/SDWebImage
1. Download and put it in the project.
2. Add: mapkit. Framework
3. # import "uiimageview + webcache. h"
4. Use:

UIImageView *asyncImage = [[UIImageView alloc] init];

[asyncImage setImageWithURL:[NSURL URLWithString:@www.istar.name/....]];

 

Use third-party Libraries

Egoimageloading is a large number of asynchronous third-party image loading classes in my project. You can find and download it on git. The link is as follows. Also,
The well-known pull-down refresh egorefreshtableheaderview is also written by this person.

Https://github.com/enormego/EGOImageLoading
(Run the demo program after downloading xcode will prompt that you cannot find the egocache. h header file, you can download the https://github.com/enormego/EGOCache In This Place)

The usage can be referred to the demo program in it. It is very simple. You just need to tell it the imageurl, and nothing else will be taken care of. It will help you load it asynchronously, and cache processing will also be done...

? 1234 // set the default placeholder image myegoimageview. placeholderimage = [uiimage imagenamed: @ "placeholder.png"]; // you can specify the URL of the image, done myegoimageview. imageurl = [nsurl urlwithstring: @ "http://simg.cocoachina.com/201111220746561330.jpg"];

 

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.