//1. NSData dataWithContentsOfURL // [self.icon setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:tempUrl]]]; //2. dispath形式添加到异步处理 // [self imageDownLoadByUrlASYNC:tempUrl Complete:^(UIImage *image) { // [self.icon setImage:image]; // }]; //3. 当前我所选用的方式 边下载边加载的方式 用的CGImageRef imageWithCGImage _request = [[NSURLRequest alloc] initWithURL:tempUrl]; _conn = [[NSURLConnection alloc] initWithRequest:_request delegate:self]; _incrementallyImgSource = CGImageSourceCreateIncremental(NULL); _recieveData = [[NSMutableData alloc] init]; _isLoadFinished = false; self.icon.alpha = .5; self.lblTitle.alpha = .5; [self.lblTitle setText:appName];
第一种方式,是基本上很少有人用的 是最基础的方式 这种方式有个问题 就是网络不好的情况下会卡主线程,导致程序假死
第二种方式,请款这段实现代码
// //-(void)imageDownLoadByUrlASYNC:(NSURL *)url Complete:(complete)finished //{ // //异步并列执行 // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // UIImage *image = nil; // NSError *error; // NSData *responseData = [NSData dataWithContentsOfURL:url options:NSDataReadingMappedIfSafe error:&error]; // image = [UIImage imageWithData:responseData]; // //跳回主队列执行 // dispatch_async(dispatch_get_main_queue(), ^{ // //在主队列中进行ui操作 // finished(image); // }); // // }); //}虽然情况跟第一种实现一样,但是将执行代码添加到对应的异步执行中 然后再成功下载之后 获取到image之后 放到主线程执行回调 设置image
第三种方式 需要以下代码 这是我百度到的方式
#pragma mark -- NSURLConnectionDataDelegate -(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response { _expectedLeght=response.expectedContentLength; NSLog(@"expectedLength:%lld",_expectedLeght); NSString*mimeType=response.MIMEType; NSLog(@"MIMETYPE%@",mimeType); NSArray*arr=[mimeType componentsSeparatedByString:@"/"]; if(arr.count<1||![[arr objectAtIndex:0] isEqual:@"image"]) { NSLog(@"notaimageurl"); [connection cancel]; _conn=nil; } } -(void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error { NSLog(@"Connection%@error,errorinfo:%@",connection,error); } -(void)connectionDidFinishLoading:(NSURLConnection*)connection { NSLog(@"ConnectionLoadingFinished!!!"); //ifdownloadimagedatanotcomplete,createfinalimage if(!_isLoadFinished){ CGImageSourceUpdateData(_incrementallyImgSource,(CFDataRef)_recieveData,_isLoadFinished); CGImageRef imageRef=CGImageSourceCreateImageAtIndex(_incrementallyImgSource,0,NULL); UIImage * image=[UIImage imageWithCGImage:imageRef]; [self.icon setImage:image]; CGImageRelease(imageRef); } } -(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data { [_recieveData appendData:data]; _isLoadFinished=false;if(_expectedLeght==_recieveData.length){ _isLoadFinished=true; } CGImageSourceUpdateData(_incrementallyImgSource,(CFDataRef)_recieveData,_isLoadFinished); CGImageRef imageRef=CGImageSourceCreateImageAtIndex(_incrementallyImgSource,0,NULL); UIImage * image=[UIImage imageWithCGImage:imageRef]; [self.icon setImage:image]; CGImageRelease(imageRef); }
原文地址:http://blog.csdn.net/zhanglei1239/article/details/41892775