首页 > 其他 > 详细

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

时间:2014-03-16 03:53:43      阅读:454      评论:0      收藏:0      [点我收藏+]
 本文转载至 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];

   }

}

封装的一个用来下载图片的类,布布扣,bubuko.com

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

原文:http://www.cnblogs.com/Camier-myNiuer/p/3601797.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!