zoukankan      html  css  js  c++  java
  • 封装的一个用来下载图片的类

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

       }

    }

  • 相关阅读:
    20200414:mysql原子性和持久性怎么保证
    20200417:说说redis的rdb原理。假设服务器的内存8g,redis父进程占用了6g,子进程fork父进程后,子父进程总共占用内存12g,如何解决内存不足的问题?(挖)
    [九省联考2018]秘密袭击coat
    CF1158F Density of subarrays
    忘情
    [IOI2018] meetings 会议
    [AGC013E] Placing Squares
    [八省联考2018]林克卡特树
    [NOI2016] 国王饮水记
    [十二省联考 2019]皮配
  • 原文地址:https://www.cnblogs.com/Camier-myNiuer/p/3601797.html
Copyright © 2011-2022 走看看