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


     本文转载至 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];

       }

    }

  • 相关阅读:
    golang 二进制转十进制实现方式
    mysql select column default value if is null
    MySQL 忘记root密码解决方法,基于Ubuntu 14.10
    ecshop 修改记录20150710
    mac os x 10.9.3 升级到10.10.4 记录
    View requires API level 14 (current min is 8): <GridLayout>
    android开发过程中遇到的坑
    如何为网站设置站点图标
    seq 显示00 01的格式
    lvs realserver 配置VIP
  • 原文地址:https://www.cnblogs.com/Camier-myNiuer/p/3601797.html
Copyright © 2020-2023  润新知