这两天一直研究多线程,网上找了个例子是多线程下载图片,自己将这个例子改成了单独的线程类,本来是向写成同时下载两张图片的,但是目前还未成功,先将下载一张图片的demo分享下,忘大家多多指点。本人QQ715425534-----------欢迎大家指点
//viewController.h/
//
// ThreadViewController.h
// Thread
//
// Created by y on 13-5-9.
// Copyright (c) 2013年 www.Zparkedu.com. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "Thread.h"
@interface ThreadViewController :UIViewController<ControControllerDelegate>
@property (retain,nonatomic) Thread *thread;
@property (retain,nonatomic) IBOutletUIImageView *imageOne;
@property (retain,nonatomic) IBOutletUIImageView *imageTwo;
@property (retain,nonatomic) NSData *downloadData;
@property (retain,nonatomic) IBOutletUIActivityIndicatorView *activityIndicatorOne;
-(IBAction)downloadOne:(id)sender;
@end
/viewController.m/
//
// ThreadViewController.m
// Thread
//
// Created by y on 13-5-9.
// Copyright (c) 2013年 www.Zparkedu.com. All rights reserved.
//
#import "ThreadViewController.h"
@interface ThreadViewController ()
@end
@implementation ThreadViewController
@synthesize imageOne,imageTwo,downloadData,thread,activityIndicatorOne;
- (void)viewDidLoad
{
[superviewDidLoad];
self.title =@"多线程Demo";
thread = [[Threadalloc]init];
self.activityIndicatorOne.hidesWhenStopped =YES;
}
-(void)viewDidUnload
{
[thread release];
[superviewDidUnload];
}
-(IBAction)downloadOne:(id)sender
{
[activityIndicatorOnestartAnimating];
NSString *urlString =@"http://h.hiphotos.baidu.com/album/w%3D2048/sign=d54ead720824ab18e016e63701c2e7cd/8b82b9014a90f603f4a5d4133812b31bb051ed49.jpg";
[self.threadcreatSubThread:urlString];
self.thread.delegate =self;
}
#pragma mark - ControControllerDelegate
-(void)sendResponsedataTOController:(NSData *)responsedata
{
NSLog(@"ControllerREsponsedata:%@",responsedata);
if (responsedata != nil) {
[self.activityIndicatorOnestopAnimating];
self.downloadData = responsedata;
NSLog(@"downloadData:%@",downloadData);
self.imageOne.image = [UIImageimageWithData:downloadData];
}
}
- (void)didReceiveMemoryWarning
{
[superdidReceiveMemoryWarning];
}
- (void)dealloc {
[imageOne release];
[imageTwo release];
[UIActivityIndicatorViewrelease];
[superdealloc];
}
@end
/线程类Thread.h///
//
// Thread.h
// Thread
//
// Created by y on 13-5-9.
// Copyright (c) 2013年 www.Zparkedu.com. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "ASIHTTPRequest.h"
#import "ASINetworkQueue.h"
@protocol ControControllerDelegate;
@protocol ControControllerDelegate <NSObject>
@required
-(void)sendResponsedataTOController:(NSData*)responsedata;
@end
@interface Thread :NSObject
@property (nonatomic,assign) id<ControControllerDelegate>delegate;
@property (retain,nonatomic) ASINetworkQueue *networkQueue;
//创建子线程
-(void)creatSubThread:(NSString*)urlString;
//子线程的方法
-(void)subThreadMethod:(NSURL*)url;
@end
/线程类Thread.m///
//
// Thread.m
// Thread
//
// Created by y on 13-5-9.
// Copyright (c) 2013年 www.Zparkedu.com. All rights reserved.
//
#import "Thread.h"
@implementation Thread
//创建子线程
-(void)creatSubThread:(NSString*)urlString
{
NSURL *url = [NSURLURLWithString:urlString];
[NSThread detachNewThreadSelector:@selector(subThreadMethod:)toTarget:selfwithObject:url];
}
//子线程的方法
-(void)subThreadMethod:(NSURL*)url
{
NSLog(@"url:%@",url);
//请求部分
ASIHTTPRequest *request = [ASIHTTPRequestrequestWithURL:url];
[request startSynchronous];
//请求失败时返回的错误信息
NSError *error = [request error];
NSLog(@"error:%@",error);
//若请求成功就存储数据
if (!error) {
NSData *responsedata = [request responseData];
NSLog(@"responsedata:%@",responsedata);
//判断是否遵循协议
if ([self.delegateconformsToProtocol:@protocol(ControControllerDelegate)])
{
NSLog(@"123responsedata:%@",responsedata);
[self.delegatesendResponsedataTOController:responsedata];
}
}
}
@end