-(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];
本文介绍了一个iOS应用中处理头像下载的过程。通过使用多线程技术,在后台下载头像图片,并将其保存到本地文件系统中。具体实现包括定义图片处理函数`processDownloadImageData:`用于保存接收到的图片数据,以及`loadImagebyThread:`函数用于异步加载图片。
4172

被折叠的 条评论
为什么被折叠?



