封装的一个用来下载图片的类

 本文转载至 http://blog.csdn.net/zaitianaoxiang/article/details/6658347
 

// ImageDownloader

// Created by Tracy E on 10-9-5.

// Copyright 2010 tracy.cpp@gmail.com. All rights reserved.

//

@protocol ImageDownloaderDelegate;

@interface ImageDownloader : NSObject

{

   NSString *imageURL;

   NSMutableData *activeDownload;

   NSURLConnection *imageConnection;

   id <ImageDownloaderDelegate> delegate;

}

@property (nonatomic,retain) NSString *imageURL;

@property (nonatomic,assign) id <ImageDownloaderDelegate> delegate;

@property (nonatomic,retain) NSMutableData *activeDownload;

@property (nonatomic,retain) NSURLConnection *imageConnection;

- (void)startDownload;

- (void)cancelDownload;

@end

@protocol ImageDownloaderDelegate

- (void)downloader:(ImageDownloader *)downloader DidFinishDownloadImage:(UIImage *)image;

@end

//

// ImageDownloader

// Created by Tracy E on 10-9-5.

// Copyright 2010 tracy.cpp@gmail.com. All rights reserved.

//

#import "ImageDownloader.h"

@implementation ImageDownloader

@synthesize imageURL;

@synthesize delegate;

@synthesize activeDownload;

@synthesize imageConnection;

#pragma mark

- (void)dealloc

{

   [activeDownload release];

   [imageConnection cancel];

   [imageConnection release];

   [super dealloc];

}

- (void)startDownload

{

   self.activeDownload = [NSMutableData data];

    // alloc+init and start an NSURLConnection; release on completion/failure

   NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:

   [NSURLRequest requestWithURL:

   [NSURL URLWithString:imageURL]] delegate:self];

   self.imageConnection = conn;

   [conn release];

}

- (void)cancelDownload

{

   [self.imageConnection cancel];

   self.imageConnection =nil;

   self.activeDownload =nil;

}

#pragma mark -

#pragma mark Download support (NSURLConnectionDelegate)

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

   [self.activeDownload appendData:data];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

    // Clear the activeDownload property to allow later attempts

   self.activeDownload =nil;

    // Release the connection now that it's finished

   self.imageConnection =nil;

}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    // Set appIcon and clear temporary data/image

   UIImage *image = [[UIImage alloc] initWithData:self.activeDownload];

    // call our delegate and tell it that our icon is ready for display

   [delegate downloader:self DidFinishDownloadImage:image]

   self.activeDownload =nil;

   [image release];

    // Release the connection now that it's finished

   self.imageConnection =nil;

}

@end

//use the imageDownloader

- (void)viewDidLoad{

    //....

   ImageDownloader *downloader = [[ImageDownloader alloc] init];

   [downloader setDelegate:self];

   downloader.imageURL = imageURLString;

   [downloader startDownload];

   [downloader release];

    //....

}

- (void)downloader:(ImageDownloader *)downloader DidFinishDownloadImage:(UIImage *)image{

    //image did finish download, receive the image here

   if (downloader !=nil) {

      [imageView setImage:image];

   }

}

原文地址:https://www.cnblogs.com/Camier-myNiuer/p/3601797.html