2012-03-26 70 views

回答

1
#import <Foundation/Foundation.h> 

@protocol DownLoadImageDelegate<NSObject> 
@required 
-(void)ImageDownLoaded:(UIImage*)image; 

@end 


@interface DownLoadImage : NSObject 
{ 

    id<DownLoadImageDelegate> mDelegate; 
    NSURLConnection *mConnection; 
    NSMutableData *mData; 
} 

@property(nonatomic,retain) id<DownLoadImageDelegate> delegate; 
@end 

//InViewController class 

DownLoadImage *imageDownLoad = [[DownLoadImage alloc]init]; 
imageDownLoad.delegate=self; 
//and implement the delegate method declared in DownLoadImage class in ViewController class as 

-(void)ImageDownLoaded:(UIImage *)image 
{ 
    NSLog(@"Image%@",image); 
    [mImageView setImage:image]; 
} 
+0

以及如何调用此函数? – 2012-04-05 14:34:15

+0

我想我明白了,我需要将这些方法设置为公开吗? – 2012-04-10 11:44:52

+0

在DowmLoadImage类中,一旦完成下载,就调用委托方法,如下所示: - (void)connectionDidFinishLoading:(NSURLConnection *)连接UIImage * image = [[UIImage alloc] initWithData:mData]; \t if(mDelegate && [mDelegate respondsToSelector:@selector(ImageDownLoaded:)]){ \t \t [mDelegate ImageDownLoaded:image]; \t} \t [mConnection release]; \t [image release]; \t [mData release]; } – sample 2012-04-11 06:26:01

相关问题