NSURLConnection异步请求
2014-10-12 23:07
25 查看
from:/article/9615294.html
NSURLConnection sendAsynchronousRequest 函数出现在ios5以后,为了兼容老版的ios (4.x) 机器,可以用AEURLConnection替代
处理例子:
[cpp] view
plaincopy
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
NSURLConnection sendAsynchronousRequest 函数出现在ios5以后,为了兼容老版的ios (4.x) 机器,可以用AEURLConnection替代
处理例子:
[cpp] view
plaincopy
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
相关文章推荐
- 一个异步网络请求的坑:关于NSURLConnection和NSRunLoopCommonModes
- NSURLConnection和NSMutableURLRequest 实现同步、异步请求
- NSURLConnection同步与异步请求
- NSURLConnection 异步发送 HTTP 请求的代码
- IOS开发笔记-NSURLConnection同步请求和异步请求以及get方法,post方法
- 一个异步网络请求的坑:关于NSURLConnection和NSRunLoopCommonModes
- NSURLConnection同步与异步请求
- 一个异步网络请求的坑:关于NSURLConnection和NSRunLoopCommonModes
- NSURLConnection同步与异步请求 问题
- NSURLConnection 异步发送 HTTP 请求的代码
- iOS网络: NSURLConnection进行异步请求
- [IOS_HTTP]NSURLConnection同步与异步请求
- ios- NSURLRequest缓存策略以及NSURLConnection发送异步请求
- NSURLConnection的异步请求方式
- NSURLConnection同步与异步请求
- 一个异步网络请求的坑:关于NSURLConnection和NSRunLoopCommonModes
- NSURLConnection和NSMutableURLRequest 实现同步、异步请求
- iOS 网络开发NSURLConnection——使用block回调方法发送异步请求
- 一个异步网络请求的坑:关于NSURLConnection和NSRunLoopCommonModes
- 利用NSURLConnection发送异步请求时各个参数的详解: