Comparison of loading network images for IOS
// 1. NSData dataWithContentsOfURL // [self. icon setImage: [UIImage imageWithData: [NSData dataWithContentsOfURL: tempUrl]; // 2. add to asynchronous processing in the form of 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];
The first method is that few people use the most basic method. The problem is that when the network is poor, the main thread will be stuck, causing the program to be suspended.
The second method is to apply for this implementation code.
////-(Void) imageDownLoadByUrlASYNC :( NSURL *) url Complete :( complete) finished // {// Asynchronous Parallel Execution // dispatch_async (dispatch_get_global_queue (bytes, 0 ), ^ {// UIImage * image = nil; // NSError * error; // NSData * responseData = [NSData dataWithContentsOfURL: url options: NSDataReadingMappedIfSafe error: & error]; // image = [UIImage imageWithData: responseData]; // jump back to the main queue for execution // dispatch_async (dispatch_get_main_queue (), ^ {// perform ui operations in the main queue column // finished (image );//});////});//}Although the situation is the same as that of the first implementation, the Execution Code is added to the corresponding asynchronous execution, and then downloaded successfully. After obtaining the image, it is placed in the main thread to execute the callback settings image.
The third method requires the following code. This is my Baidu method.
#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);}
This method is very useful after I tested it, but I don't know if there will be any bugs. It's just a new version and the user experience will increase accordingly.