您的位置:首页 > 理论基础 > 计算机网络

根据url多线程请求载入网络图片

2012-08-01 16:05 399 查看
-(void) processDownloadImageData:(NSData *) imageData
{
NSString *imagePath = [NSString
stringWithFormat:@"%@/avatar%@.png",document,pathuserID];
if (![imageData writeToFile:imagePath
atomically:YES]) {
NSLog(@"save imageData Error with path:%@",imagePath);
}
//do anything if you want
}//接收到图片后的处理函数

-(void) loadImagebyThread:(NSString *) imageUrlStr
{
NSAutoreleasePool *pool = [[NSAutoreleasePool
alloc] init];
NSURL *imgUrl = [NSURL
URLWithString:imageUrlStr];
NSData *imgData = [NSData
dataWithContentsOfURL:imgUrl];
if (imgData) {
[self performSelectorOnMainThread:@selector(processDownloadImageData:)
withObject:imgData waitUntilDone:NO];
}
[pool release];
}//请求图片的线程函数

在某个需要的地方添加多线程:

NSString *avatar_large_urlStr = [profileDic
objectForKey:@"avatar_large"];
[NSThread
detachNewThreadSelector:@selector(loadImagebyThread:)
toTarget:self withObject:avatar_large_urlStr];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: