Comparison of loading network images for IOS

Source: Internet
Author: User

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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.