ios4.x sendAsynchronousRequest 替代方案

NSURLConnection  sendAsynchronousRequest 函数出现在ios5以后,为了兼容老版的ios (4.x) 机器,可以用AEURLConnection替代

处理例子:

        NSURLRequest* req = [NSURLRequest requestWithURL:url];
        
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0
        [AEURLConnection sendAsynchronousRequest:req
                                           queue:_downloadQueue
                               completionHandler:^(NSURLResponse * res, NSData * data, NSError * error)
#endif
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_5_0
        [NSURLConnection sendAsynchronousRequest:req
                                           queue:_downloadQueue
                               completionHandler:^(NSURLResponse * res, NSData * data, NSError * error)
        
#endif
        {
                                   if (data.length && !error) {
                                       dispatch_async(_queue, ^(void) {
                                           [_imageCache setObject:data forKey:cacheKey];
                                           
                                           UIImage* srcImage = [UIImage imageWithData:data];
                                           UIImage* image = [self _resizeImage:srcImage bounds:bounds options:options];
                                           dispatch_async(dispatch_get_main_queue(), ^(void) {
                                               comp(image, nil);
                                           });
                                       });
                                   } else {
                                       // Request failed
                                       dispatch_async(dispatch_get_main_queue(), ^{
                                           comp(nil, error); 
                                       });
                                   }
                               }];
	});


AEURLConnection 源码来源

https://github.com/adamjernst/AEURLConnection

你可能感兴趣的:(ios4.x sendAsynchronousRequest 替代方案)