AFNetWorking 的简单使用
转:http://blog.csdn.net/marujunyy/article/details/18424711
由于ASIHTTPRequest 不再更新了,不能使用block感觉不太好用;最后选择了AFNetWorking,并做了进一步的封装。
需要导入的framework:CFNetwork、Security、SystemConfiguration、MobileCoreServices
GitHub:https://github.com/AFNetworking/AFNetworking
最新的版本为2.0支持iOS 6.0以上系统,在iOS 5上可能报错:Property
with 'retain (or strong)' attribute must be of object type
下面是封装的类:
// HttpManager.h
- //
- // HttpManager.h
- // HLMagic
- //
- // Created by marujun on 14-1-17.
- // Copyright (c) 2014年 jizhi. All rights reserved.
- //
- #import <Foundation/Foundation.h>
- #import <CommonCrypto/CommonDigest.h>
- #import "AFNetworking.h"
- #import "Reachability.h"
- @interface NSString (HttpManager)
- - (NSString *)md5;
- - (NSString *)encode;
- - (NSString *)decode;
- - (NSString *)object;
- @end
- @interface NSObject (HttpManager)
- - (NSString *)json;
- @end
- @interface HttpManager : NSObject
- + (HttpManager *)defaultManager;
- /* -------判断当前的网络类型----------
- 1、NotReachable - 没有网络连接
- 2、ReachableViaWWAN - 移动网络(2G、3G)
- 3、ReachableViaWiFi - WIFI网络
- */
- + (NetworkStatus)networkStatus;
- //GET 请求
- - (void)getRequestToUrl:(NSString *)url params:(NSDictionary *)params complete:(void (^)(BOOL successed, NSDictionary *result))complete;
- - (void)getCacheToUrl:(NSString *)url params:(NSDictionary *)params complete:(void (^)(BOOL successed, NSDictionary *result))complete; //请求失败时使用缓存数据
- //POST 请求
- - (void)postRequestToUrl:(NSString *)url params:(NSDictionary *)params complete:(void (^)(BOOL successed, NSDictionary *result))complete;
- /*
- files : 需要上传的文件数组,数组里为多个字典
- 字典里的key:
- 1、name: 文件名称(如:demo.jpg)
- 2、file: 文件 (支持四种数据类型:NSData、UIImage、NSURL、NSString)NSURL、NSString为文件路径
- 3、type: 文件类型(默认:image/jpeg)
- 示例: @[@{@"file":_headImg.currentBackgroundImage,@"name":@"head.jpg"}];
- */
- //AFHTTPRequestOperation可以暂停、重新开启、取消 [operation pause]、[operation resume];、[operation cancel];
- - (AFHTTPRequestOperation *)uploadToUrl:(NSString *)url
- params:(NSDictionary *)params
- files:(NSArray *)files
- complete:(void (^)(BOOL successed, NSDictionary *result))complete;
- //可以查看进度 process_block
- - (AFHTTPRequestOperation *)uploadToUrl:(NSString *)url
- params:(NSDictionary *)params
- files:(NSArray *)files
- process:(void (^)(NSInteger writedBytes, NSInteger totalBytes))process
- complete:(void (^)(BOOL successed, NSDictionary *result))complete;
- /*
- filePath : 下载文件的存储路径
- response : 接口返回的不是文件而是json数据
- process : 进度
- */
- - (AFHTTPRequestOperation *)downloadFromUrl:(NSString *)url
- filePath:(NSString *)filePath
- complete:(void (^)(BOOL successed, NSDictionary *response))complete;
- - (AFHTTPRequestOperation *)downloadFromUrl:(NSString *)url
- params:(NSDictionary *)params
- filePath:(NSString *)filePath
- process:(void (^)(NSInteger readBytes, NSInteger totalBytes))process
- complete:(void (^)(BOOL successed, NSDictionary *response))complete;
- @end
// "HttpManager.m"
- //
- // HttpManager.m
- // HLMagic
- //
- // Created by marujun on 14-1-17.
- // Copyright (c) 2014年 jizhi. All rights reserved.
- //
- #import "HttpManager.h"
- @implementation NSString (HttpManager)
- - (NSString *)md5
- {
- if(self == nil || [self length] == 0){
- return nil;
- }
- const char *value = [self UTF8String];
- unsigned char outputBuffer[CC_MD5_DIGEST_LENGTH];
- CC_MD5(value, (CC_LONG)strlen(value), outputBuffer);
- NSMutableString *outputString = [[NSMutableString alloc] initWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
- for(NSInteger count = 0; count < CC_MD5_DIGEST_LENGTH; count++){
- [outputString appendFormat:@"%02x",outputBuffer[count]];
- }
- return outputString;
- }
- - (NSString *)encode
- {
- NSString *outputStr = (NSString *)
- CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
- (CFStringRef)self,
- NULL,
- NULL,
- kCFStringEncodingUTF8));
- return outputStr;
- }
- - (NSString *)decode
- {
- NSMutableString *outputStr = [NSMutableString stringWithString:self];
- [outputStr replaceOccurrencesOfString:@"+" withString:@" " options:NSLiteralSearch range:NSMakeRange(0, [outputStr length])];
- return [outputStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
- }
- - (id)object
- {
- id object = nil;
- @try {
- NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];;
- object = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
- }
- @catch (NSException *exception) {
- NSLog(@"%s [Line %d] JSON字符串转换成对象出错了-->\n%@",__PRETTY_FUNCTION__, __LINE__,exception);
- }
- @finally {
- }
- return object;
- }
- @end
- @implementation NSObject (HttpManager)
- - (NSString *)json
- {
- NSString *jsonStr = @"";
- @try {
- NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:0 error:nil];
- jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
- }
- @catch (NSException *exception) {
- NSLog(@"%s [Line %d] 对象转换成JSON字符串出错了-->\n%@",__PRETTY_FUNCTION__, __LINE__,exception);
- }
- @finally {
- }
- return jsonStr;
- }
- @end
- @interface HttpManager ()
- {
- AFHTTPRequestOperationManager *operationManager;
- }
- @end
- @implementation HttpManager
- - (id)init{
- self = [super init];
- if (self) {
- operationManager = [AFHTTPRequestOperationManager manager];
- operationManager.responseSerializer.acceptableContentTypes = nil;
- NSURLCache *urlCache = [NSURLCache sharedURLCache];
- [urlCache setMemoryCapacity:5*1024*1024]; /* 设置缓存的大小为5M*/
- [NSURLCache setSharedURLCache:urlCache];
- }
- return self;
- }
- + (HttpManager *)defaultManager
- {
- static dispatch_once_t pred = 0;
- __strong static id defaultHttpManager = nil;
- dispatch_once( &pred, ^{
- defaultHttpManager = [[self alloc] init];
- });
- return defaultHttpManager;
- }
- - (void)getRequestToUrl:(NSString *)url params:(NSDictionary *)params complete:(void (^)(BOOL successed, NSDictionary *result))complete
- {
- [self requestToUrl:url method:@"GET" useCache:NO params:params complete:complete];
- }
- - (void)getCacheToUrl:(NSString *)url params:(NSDictionary *)params complete:(void (^)(BOOL successed, NSDictionary *result))complete
- {
- [self requestToUrl:url method:@"GET" useCache:YES params:params complete:complete];
- }
- - (void)postRequestToUrl:(NSString *)url params:(NSDictionary *)params complete:(void (^)(BOOL successed, NSDictionary *result))complete
- {
- [self requestToUrl:url method:@"POST" useCache:NO params:params complete:complete];
- }
- - (void)requestToUrl:(NSString *)url method:(NSString *)method useCache:(BOOL)useCache
- params:(NSDictionary *)params complete:(void (^)(BOOL successed, NSDictionary *result))complete
- {
- params = [[HttpManager getRequestBodyWithParams:params] copy];
- AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
- NSMutableURLRequest *request = [serializer requestWithMethod:method URLString:url parameters:params error:nil];
- [request setTimeoutInterval:10];
- if (useCache) {
- [request setCachePolicy:NSURLRequestReturnCacheDataElseLoad];
- }
- void (^requestSuccessBlock)(AFHTTPRequestOperation *operation, id responseObject) = ^(AFHTTPRequestOperation *operation, id responseObject) {
- [self showMessageWithOperation:operation method:method params:params];
- complete ? complete(true,responseObject) : nil;
- };
- void (^requestFailureBlock)(AFHTTPRequestOperation *operation, NSError *error) = ^(AFHTTPRequestOperation *operation, NSError *error) {
- [self showMessageWithOperation:operation method:method params:params];
- complete ? complete(false,nil) : nil;
- };
- AFHTTPRequestOperation *operation = nil;
- if (useCache) {
- operation = [self cacheOperationWithRequest:request success:requestSuccessBlock failure:requestFailureBlock];
- }else{
- operation = [operationManager HTTPRequestOperationWithRequest:request success:requestSuccessBlock failure:requestFailureBlock];
- }
- [operationManager.operationQueue addOperation:operation];
- }
- - (AFHTTPRequestOperation *)cacheOperationWithRequest:(NSURLRequest *)urlRequest
- success:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
- failure:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
- {
- AFHTTPRequestOperation *operation = [operationManager HTTPRequestOperationWithRequest:urlRequest success:^(AFHTTPRequestOperation *operation, id responseObject){
- NSCachedURLResponse *cachedURLResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:urlRequest];
- //store in cache
- cachedURLResponse = [[NSCachedURLResponse alloc] initWithResponse:operation.response data:operation.responseData userInfo:nil storagePolicy:NSURLCacheStorageAllowed];
- [[NSURLCache sharedURLCache] storeCachedResponse:cachedURLResponse forRequest:urlRequest];
- success(operation,responseObject);
- }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- if (error.code == kCFURLErrorNotConnectedToInternet) {
- NSCachedURLResponse *cachedResponse = [[NSURLCache sharedURLCache] cachedResponseForRequest:urlRequest];
- if (cachedResponse != nil && [[cachedResponse data] length] > 0) {
- success(operation, cachedResponse.data);
- } else {
- failure(operation, error);
- }
- } else {
- failure(operation, error);
- }
- }];
- return operation;
- }
- - (AFHTTPRequestOperation *)uploadToUrl:(NSString *)url
- params:(NSDictionary *)params
- files:(NSArray *)files
- complete:(void (^)(BOOL successed, NSDictionary *result))complete
- {
- return [self uploadToUrl:url params:params files:files process:nil complete:complete];
- }
- - (AFHTTPRequestOperation *)uploadToUrl:(NSString *)url
- params:(NSDictionary *)params
- files:(NSArray *)files
- process:(void (^)(NSInteger writedBytes, NSInteger totalBytes))process
- complete:(void (^)(BOOL successed, NSDictionary *result))complete
- {
- params = [[HttpManager getRequestBodyWithParams:params] copy];
- FLOG(@"post request url: %@ \npost params: %@",url,params);
- AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
- NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
- for (NSDictionary *fileItem in files) {
- id value = [fileItem objectForKey:@"file"]; //支持四种数据类型:NSData、UIImage、NSURL、NSString
- NSString *name = @"file"; //字段名称
- NSString *fileName = [fileItem objectForKey:@"name"]; //文件名称
- NSString *mimeType = [fileItem objectForKey:@"type"]; //文件类型
- mimeType = mimeType ? mimeType : @"image/jpeg";
- if ([value isKindOfClass:[NSData class]]) {
- [formData appendPartWithFileData:value name:name fileName:fileName mimeType:mimeType];
- }else if ([value isKindOfClass:[UIImage class]]) {
- if (UIImagePNGRepresentation(value)) { //返回为png图像。
- [formData appendPartWithFileData:UIImagePNGRepresentation(value) name:name fileName:fileName mimeType:mimeType];
- }else { //返回为JPEG图像。
- [formData appendPartWithFileData:UIImageJPEGRepresentation(value, 0.5) name:name fileName:fileName mimeType:mimeType];
- }
- }else if ([value isKindOfClass:[NSURL class]]) {
- [formData appendPartWithFileURL:value name:name fileName:fileName mimeType:mimeType error:nil];
- }else if ([value isKindOfClass:[NSString class]]) {
- [formData appendPartWithFileURL:[NSURL URLWithString:value] name:name fileName:fileName mimeType:mimeType error:nil];
- }
- }
- } error:nil];
- AFHTTPRequestOperation *operation = nil;
- operation = [operationManager HTTPRequestOperationWithRequest:request
- success:^(AFHTTPRequestOperation *operation, id responseObject) {
- FLOG(@"post responseObject: %@",responseObject);
- if (complete) {
- complete(true,responseObject);
- }
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- FLOG(@"post error : %@",error);
- if (complete) {
- complete(false,nil);
- }
- }];
- [operation setUploadProgressBlock:^(NSUInteger bytesWritten, NSInteger totalBytesWritten, NSInteger totalBytesExpectedToWrite) {
- FLOG(@"upload process: %.2d%% (%ld/%ld)",100*totalBytesWritten/totalBytesExpectedToWrite,(long)totalBytesWritten,(long)totalBytesExpectedToWrite);
- if (process) {
- process(totalBytesWritten,totalBytesExpectedToWrite);
- }
- }];
- [operation start];
- return operation;
- }
- - (AFHTTPRequestOperation *)downloadFromUrl:(NSString *)url
- filePath:(NSString *)filePath
- complete:(void (^)(BOOL successed, NSDictionary *response))complete
- {
- return [self downloadFromUrl:url params:nil filePath:filePath process:nil complete:complete];
- }
- - (AFHTTPRequestOperation *)downloadFromUrl:(NSString *)url
- params:(NSDictionary *)params
- filePath:(NSString *)filePath
- process:(void (^)(NSInteger readBytes, NSInteger totalBytes))process
- complete:(void (^)(BOOL successed, NSDictionary *response))complete
- {
- params = [[HttpManager getRequestBodyWithParams:params] copy];
- AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
- NSMutableURLRequest *request = [serializer requestWithMethod:@"GET" URLString:url parameters:params error:nil];
- FLOG(@"get request url: %@",[request.URL.absoluteString decode]);
- AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
- operation.responseSerializer.acceptableContentTypes = nil;
- NSString *tmpPath = [filePath stringByAppendingString:@".tmp"];
- operation.outputStream=[[NSOutputStream alloc] initToFileAtPath:tmpPath append:NO];
- [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
- NSArray *mimeTypeArray = @[@"text/html", @"application/json"];
- NSError *moveError = nil;
- if ([mimeTypeArray containsObject:operation.response.MIMEType]) {
- //返回的是json格式数据
- responseObject = [NSData dataWithContentsOfFile:tmpPath];
- responseObject = [NSJSONSerialization JSONObjectWithData:responseObject options:2 error:nil];
- [[NSFileManager defaultManager] removeItemAtPath:tmpPath error:nil];
- FLOG(@"get responseObject: %@",responseObject);
- }else{
- [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
- [[NSFileManager defaultManager] moveItemAtPath:tmpPath toPath:filePath error:&moveError];
- }
- if (complete && !moveError) {
- complete(true,responseObject);
- }else{
- complete?complete(false,responseObject):nil;
- }
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- FLOG(@"get error : %@",error);
- [[NSFileManager defaultManager] removeItemAtPath:tmpPath error:nil];
- if (complete) {
- complete(false,nil);
- }
- }];
- [operation setDownloadProgressBlock:^(NSUInteger bytesRead, NSInteger totalBytesRead, NSInteger totalBytesExpectedToRead) {
- FLOG(@"download process: %.2d%% (%ld/%ld)",100*totalBytesRead/totalBytesExpectedToRead,(long)totalBytesRead,(long)totalBytesExpectedToRead);
- if (process) {
- process(totalBytesRead,totalBytesExpectedToRead);
- }
- }];
- [operation start];
- return operation;
- }
- + (NSMutableDictionary *)getRequestBodyWithParams:(NSDictionary *)params
- {
- NSMutableDictionary *requestBody = params?[params mutableCopy]:[[NSMutableDictionary alloc] init];
- for (NSString *key in [params allKeys]){
- id value = [params objectForKey:key];
- if ([value isKindOfClass:[NSDate class]]) {
- [requestBody setValue:@([value timeIntervalSince1970]*1000) forKey:key];
- }
- if ([value isKindOfClass:[NSDictionary class]] || [value isKindOfClass:[NSArray class]]) {
- [requestBody setValue:[value json] forKey:key];
- }
- }
- NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
- if (token){
- [requestBody setObject:token forKey:@"token"];
- }
- [requestBody setObject:@"ios" forKey:@"genus"];
- return requestBody;
- }
- + (NetworkStatus)networkStatus
- {
- Reachability *reachability = [Reachability reachabilityWithHostname:@"www.apple.com"];
- // NotReachable - 没有网络连接
- // ReachableViaWWAN - 移动网络(2G、3G)
- // ReachableViaWiFi - WIFI网络
- return [reachability currentReachabilityStatus];
- }
- - (void)showMessageWithOperation:(AFHTTPRequestOperation *)operation method:(NSString *)method params:(NSDictionary *)params
- {
- NSString *urlAbsoluteString = [operation.request.URL.absoluteString decode];
- if ([[method uppercaseString] isEqualToString:@"GET"]) {
- FLOG(@"get request url: %@ \n",urlAbsoluteString);
- }else{
- FLOG(@"%@ request url: %@ \npost params: %@\n",[method lowercaseString],urlAbsoluteString,params);
- }
- if (operation.error) {
- FLOG(@"%@ error : %@",[method lowercaseString],operation.error);
- }else{
- FLOG(@"%@ responseObject: %@",[method lowercaseString],operation.responseObject);
- }
- // //只显示一部分url
- // NSArray *ignordUrls = @[url_originalDataDownload,url_originalDataUpload,url_originalDataUploadFinished,url_getEarliestOriginalData,url_newVersion,
- // url_saveSyncFailInfo];
- // for (NSString *ignordUrl in ignordUrls) {
- // if ([urlAbsoluteString rangeOfString:ignordUrl].length) {
- // return;
- // }
- // }
- // //弹出网络提示
- // if (!operation.error) {
- // if ([operation.responseObject objectForKey:@"msg"] && [[operation.responseObject objectForKey:@"msg"] length]) {
- // [KeyWindow showAlertMessage:[operation.responseObject objectForKey:@"msg"] callback:nil];
- // }
- // }
- // else {
- // if (operation.error.code == kCFURLErrorNotConnectedToInternet) {
- // [KeyWindow showAlertMessage:@"您已断开网络连接" callback:nil];
- // } else {
- // [KeyWindow showAlertMessage:@"服务器忙,请稍后再试" callback:nil];
- // }
- // }
- }
- @end
图片缓存类,用于缓存图片;并写了UIImageView和UIButton的扩展方法;可直接设置其对应图片的URL。
// ImageCache.h
- //
- // ImageCache.h
- // CoreDataUtil
- //
- // Created by marujun on 14-1-18.
- // Copyright (c) 2014年 jizhi. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #import <UIKit/UIImageView.h>
- #import <UIKit/UIButton.h>
- #import <objc/runtime.h>
- #define ADD_DYNAMIC_PROPERTY(PROPERTY_TYPE,PROPERTY_NAME,SETTER_NAME) \
- @dynamic PROPERTY_NAME ; \
- static char kProperty##PROPERTY_NAME; \
- - ( PROPERTY_TYPE ) PROPERTY_NAME{ \
- return ( PROPERTY_TYPE ) objc_getAssociatedObject(self, &(kProperty##PROPERTY_NAME ) ); \
- } \
- - (void) SETTER_NAME :( PROPERTY_TYPE ) PROPERTY_NAME{ \
- objc_setAssociatedObject(self, &kProperty##PROPERTY_NAME , PROPERTY_NAME , OBJC_ASSOCIATION_RETAIN); \
- } \
- @interface UIImage (ImageCache)
- @property(nonatomic, strong)NSString *lastCacheUrl;
- /* ********************----------*****************************
- 1、UIImage 的扩展方法,用于缓存图片;如果图片已下载则使用本地图片
- 2、下载完成之后会执行回调,并可查看下载进度
- ********************----------******************************/
- + (void)imageWithURL:(NSString *)url callback:(void(^)(UIImage *image))callback;
- + (void)imageWithURL:(NSString *)url
- process:(void (^)(NSInteger readBytes, NSInteger totalBytes))process
- callback:(void(^)(UIImage *image))callback;
- /*通过URL获取缓存图片在本地对应的路径*/
- + (NSString *)getImagePathWithURL:(NSString *)url;
- @end
- @interface UIImageView (ImageCache)
- @property(nonatomic, strong)NSString *lastCacheUrl;
- /*设置UIImageView的图片的URL,下载失败设置图片为空*/
- - (void)setImageURL:(NSString *)url;
- /*设置UIImageView的图片的URL,下载失败则使用默认图片设置*/
- - (void)setImageURL:(NSString *)url defaultImage:(UIImage *)defaultImage;
- /*设置UIImageView的图片的URL,下载完成之后先设置图片然后执行回调函数*/
- - (void)setImageURL:(NSString *)url callback:(void(^)(UIImage *image))callback;
- @end
- @interface UIButton (ImageCache)
- @property(nonatomic, strong)NSString *lastCacheUrl;
- /*设置按钮的图片的URL,下载失败设置图片为空*/
- - (void)setImageURL:(NSString *)url forState:(UIControlState)state;
- /*设置按钮的图片的URL,下载失败则使用默认图片设置*/
- - (void)setImageURL:(NSString *)url forState:(UIControlState)state defaultImage:(UIImage *)defaultImage;
- /*设置按钮的图片的URL,下载完成之后先设置图片然后执行回调函数*/
- - (void)setImageURL:(NSString *)url forState:(UIControlState)state callback:(void(^)(UIImage *image))callback;
- /*设置按钮的背景图片的URL,下载失败设置图片为空*/
- - (void)setBackgroundImageURL:(NSString *)url forState:(UIControlState)state;
- /*设置按钮的背景图片的URL,下载失败则使用默认图片设置*/
- - (void)setBackgroundImageURL:(NSString *)url forState:(UIControlState)state defaultImage:(UIImage *)defaultImage;
- /*设置按钮的背景图片的URL,下载完成之后先设置图片然后执行回调函数*/
- - (void)setBackgroundImageURL:(NSString *)url forState:(UIControlState)state callback:(void(^)(UIImage *image))callback;
- @end
// ImageCache.m
- //
- // ImageCache.m
- // CoreDataUtil
- //
- // Created by marujun on 14-1-18.
- // Copyright (c) 2014年 jizhi. All rights reserved.
- //
- #import "ImageCache.h"
- #import "HttpManager.h"
- static NSMutableArray *downloadTaskArray_ImageCache;
- static BOOL isDownloading_ImageCache;
- @implementation UIImage (ImageCache)
- ADD_DYNAMIC_PROPERTY(NSString *,lastCacheUrl,setLastCacheUrl);
- + (void)imageWithURL:(NSString *)url callback:(void(^)(UIImage *image))callback
- {
- [self imageWithURL:url process:nil callback:callback];
- }
- + (void)imageWithURL:(NSString *)url
- process:(void (^)(NSInteger readBytes, NSInteger totalBytes))process
- callback:(void(^)(UIImage *image))callback
- {
- if (!downloadTaskArray_ImageCache) {
- downloadTaskArray_ImageCache = [[NSMutableArray alloc] init];
- }
- NSString *filePath = [self getImagePathWithURL:url];
- if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
- UIImage *lastImage = [UIImage imageWithContentsOfFile:filePath];
- lastImage.lastCacheUrl = url?url:@"";
- callback ? callback(lastImage) : nil;
- }else{
- NSMutableDictionary *task = [[NSMutableDictionary alloc] init];
- url?[task setObject:url forKey:@"url"]:nil;
- process?[task setObject:process forKey:@"process"]:nil;
- callback?[task setObject:callback forKey:@"callback"]:nil;
- [downloadTaskArray_ImageCache addObject:task];
- [self startDownload];
- }
- }
- + (void)startDownload
- {
- if (downloadTaskArray_ImageCache.count && !isDownloading_ImageCache) {
- NSDictionary *lastObj = [downloadTaskArray_ImageCache lastObject];
- [self downloadWithURL:lastObj[@"url"] process:lastObj[@"process"] callback:lastObj[@"callback"]];
- }
- }
- + (void)downloadWithURL:(NSString *)url
- process:(void (^)(NSInteger readBytes, NSInteger totalBytes))process
- callback:(void(^)(UIImage *image))callback
- {
- NSString *filePath = [self getImagePathWithURL:url];
- NSMutableDictionary *task = [[NSMutableDictionary alloc] init];
- url?[task setObject:url forKey:@"url"]:nil;
- process?[task setObject:process forKey:@"process"]:nil;
- callback?[task setObject:callback forKey:@"callback"]:nil;
- isDownloading_ImageCache = true;
- if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
- UIImage *lastImage = [UIImage imageWithContentsOfFile:filePath];
- lastImage.lastCacheUrl = url?url:@"";
- callback ? callback(lastImage) : nil;
- [downloadTaskArray_ImageCache removeObject:task];
- isDownloading_ImageCache = false;
- [self startDownload];
- }else{
- [[HttpManager defaultManager] downloadFromUrl:url
- params:nil
- filePath:filePath
- process:process
- complete:^(BOOL successed, NSDictionary *result) {
- if (callback) {
- if (successed && !result) {
- UIImage *lastImage = [UIImage imageWithContentsOfFile:filePath];
- lastImage.lastCacheUrl = url?url:@"";
- callback ? callback(lastImage) : nil;
- }else{
- callback(nil);
- }
- }
- [downloadTaskArray_ImageCache removeObject:task];
- isDownloading_ImageCache = false;
- [self startDownload];
- }];
- }
- }
- + (NSString *)getImagePathWithURL:(NSString *)url
- {
- //先创建个缓存文件夹
- NSString *directory = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches/imgcache"];
- NSFileManager *defaultManager = [NSFileManager defaultManager];
- if (![defaultManager fileExistsAtPath:directory]) {
- [defaultManager createDirectoryAtPath:directory withIntermediateDirectories:YES attributes:nil error:nil];
- }
- return [directory stringByAppendingPathComponent:[url md5]];
- }
- @end
- @implementation UIImageView (ImageCache)
- ADD_DYNAMIC_PROPERTY(NSString *,lastCacheUrl,setLastCacheUrl);
- - (void)setImageURL:(NSString *)url
- {
- [self setImageURL:url callback:nil];
- }
- - (void)setImageURL:(NSString *)url defaultImage:(UIImage *)defaultImage
- {
- defaultImage ? self.image=defaultImage : nil;
- self.lastCacheUrl = url;
- [UIImage imageWithURL:url callback:^(UIImage *image) {
- if ([image.lastCacheUrl isEqualToString:self.lastCacheUrl]) {
- image ? self.image=image : nil;
- }
- }];
- }
- - (void)setImageURL:(NSString *)url callback:(void(^)(UIImage *image))callback
- {
- self.lastCacheUrl = url;
- [UIImage imageWithURL:url callback:^(UIImage *image) {
- if ([image.lastCacheUrl isEqualToString:self.lastCacheUrl]) {
- image ? self.image=image : nil;
- }
- callback ? callback(image) : nil;
- }];
- }
- @end
- @implementation UIButton (ImageCache)
- ADD_DYNAMIC_PROPERTY(NSString *,lastCacheUrl,setLastCacheUrl);
- - (void)setImageURL:(NSString *)url forState:(UIControlState)state
- {
- [self setImageURL:url forState:state defaultImage:nil];
- }
- - (void)setImageURL:(NSString *)url forState:(UIControlState)state defaultImage:(UIImage *)defaultImage
- {
- defaultImage ? [self setImage:defaultImage forState:state] : nil;
- self.lastCacheUrl = url;
- [UIImage imageWithURL:url callback:^(UIImage *image) {
- if ([image.lastCacheUrl isEqualToString:self.lastCacheUrl]) {
- image ? [self setImage:image forState:state] : nil;
- }
- }];
- }
- - (void)setImageURL:(NSString *)url forState:(UIControlState)state callback:(void(^)(UIImage *image))callback
- {
- self.lastCacheUrl = url;
- [UIImage imageWithURL:url callback:^(UIImage *image) {
- if ([image.lastCacheUrl isEqualToString:self.lastCacheUrl]) {
- image ? [self setImage:image forState:state] : nil;
- }
- callback ? callback(image) : nil;
- }];
- }
- - (void)setBackgroundImageURL:(NSString *)url forState:(UIControlState)state
- {
- [self setBackgroundImageURL:url forState:state defaultImage:nil];
- }
- - (void)setBackgroundImageURL:(NSString *)url forState:(UIControlState)state defaultImage:(UIImage *)defaultImage
- {
- defaultImage ? [self setBackgroundImage:defaultImage forState:state] : nil;
- self.lastCacheUrl = url;
- [UIImage imageWithURL:url callback:^(UIImage *image) {
- if ([image.lastCacheUrl isEqualToString:self.lastCacheUrl]) {
- image ? [self setBackgroundImage:image forState:state] : nil;
- }
- }];
- }
- - (void)setBackgroundImageURL:(NSString *)url forState:(UIControlState)state callback:(void(^)(UIImage *image))callback
- {
- self.lastCacheUrl = url;
- [UIImage imageWithURL:url callback:^(UIImage *image) {
- if ([image.lastCacheUrl isEqualToString:self.lastCacheUrl]) {
- image ? [self setBackgroundImage:image forState:state] : nil;
- }
- callback ? callback(image) : nil;
- }];
- }
- @end
- /* 使用示例 */
- NSString *url = @"http://b.hiphotos.baidu.com/image/w%3D2048/sign=4c2a6e019058d109c4e3aeb2e560cdbf/b812c8fcc3cec3fd6d7daa0ad488d43f87942709.jpg";
- //缓存图片
- // [UIImage imageWithURL:url process:^(NSInteger readBytes, NSInteger totalBytes) {
- // NSLog(@"下载进度 : %.0f%%",100*readBytes/totalBytes);
- // } callback:^(UIImage *image) {
- // NSLog(@"图片下载完成!");
- // }];
- //设置UIImageView的图片,下载失败则使用默认图片
- UIImageView *imageView = [[UIImageView alloc] initWithFrame:self.view.bounds];
- [imageView setImageURL:url defaultImage:[UIImage imageNamed:@"default.png"]];
- imageView.contentMode = UIViewContentModeScaleAspectFit;
- [self.view addSubview:imageView];
- //设置UIButton的图片,下载失败则使用默认图片
- UIButton *button = [[UIButton alloc] initWithFrame:self.view.bounds];
- [button setImageURL:url forState:UIControlStateNormal defaultImage:[UIImage imageNamed:@"default.png"]];
- [self.view addSubview:button];
GitHub 地址:https://github.com/marujun/DataManager
AFNetWorking 的简单使用的更多相关文章
- AFNetworking的简单使用
AFNetworking的下载地址: https://github.com/AFNetworking/AFNetworking AFNetworking的使用非常简单,创建一个类,调用一个方法就可以达 ...
- iOS开发 -------- AFNetworking实现简单的断点下载
一 实现如下效果 二 实现代码 // // ViewController.m // AFNetworking实现断点下载 // // Created by lovestarfish on 15/1 ...
- AFNetworking 简单应用
最近最学习 AFNetworking ,根据自己所学对 AFNetWorking 一些简单应用做了一下简单封装,主要有 get,post形式获取 xml,json,get 方式获取图片,下载文件,上传 ...
- IOS AFNetworking简介及使用
转:http://www.it165.net/pro/html/201405/13099.html 一AFNetworking简介AFNetworking是一个在IOS开发中使用非常多网络开源库,适用 ...
- AFNetworking源码浅析
本文将从最简单的GET请求方法的使用入手,由表及里,逐步探究AFNetworking如何封装处理原生的网络请求. 一.AFNetworking的简单使用 -(void)getDemo{ AFHTTPS ...
- AFNetworking封装-项目使用
本篇博客是接着上一篇AFNetworking源码解析的后续,如果想对AFNetworking源码有所了解. 请读一下https://www.cnblogs.com/guohai-stronger/p/ ...
- 网络编程---HTTP
URL: 什么是URL ? URL的全称是Uniform Resource Locator(统一资源定位符) 通过1个URL,能找到互联网上唯一的1个资源 URL就是资源的地址.位置,互联网上的每个资 ...
- iOS开发——网络篇——HTTP/NSURLConnection(请求、响应)、http响应状态码大全
一.网络基础 1.基本概念> 为什么要学习网络编程在移动互联网时代,移动应用的特征有几乎所有应用都需要用到网络,比如QQ.微博.网易新闻.优酷.百度地图只有通过网络跟外界进行数据交互.数据更新, ...
- iOS开发网络篇—HTTP协议
iOS开发网络篇—HTTP协议 说明:apache tomcat服务器必须占用8080端口 一.URL 1.基本介绍 URL的全称是Uniform Resource Locator(统一资源定位符) ...
随机推荐
- script是什么
script是什么 scirpt就是一个命令,可以制作一份记录输出到终端的记录.对于那些想要真实记录终端会话的人来说,这很有用.该记录可以保存并在以后再打印出来. 怎么用 默认情况下,我们可以通过在终 ...
- Java的synchronized关键字:同步机制总结
JAVA中synchronized关键字能够作为函数的修饰符,也可作为函数内的语句,也就是平时说的同步方法和同步语句块.搞清楚synchronized锁定的是哪个对象,就能帮助我们设计更安全的多线程程 ...
- HDU 1573 X问题 (中国剩余定理)
题目链接 题意 : 中文题不详述. 思路 : 中国剩余定理.求中国剩余定理中解的个数.看这里看这里 #include <stdio.h> #include <iostream> ...
- hdu 1755 A Number Puzzle
这题枚举k节省时间 ;}
- CKeditor 配置使用
CKeditor 配置使用 一.使用方法:1.在页面<head>中引入ckeditor核心文件ckeditor.js<script type="text/javascrip ...
- lintcode :Partition List 链表划分
题目: 链表划分 给定一个单链表和数值x,划分链表使得所有小于x的节点排在大于等于x的节点之前. 你应该保留两部分内链表节点原有的相对顺序. 样例 给定链表 1->4->3->2-& ...
- 【mongoDB中级篇②】索引与expain
索引的操作 数据库百分之八十的工作基本上都是查询,而索引能帮我们更快的查询到想要的数据.但是其降低了数据的写入速度,所以要权衡常用的查询字段,不必在太多字段上建立索引. 在mongoDB中默认是用bt ...
- alias 命令
功能说明:设置指令的别名. 语 法:alias[别名]=[指令名称] 参 数 :若不加任何参数,则列出目前所有的别名设置. 举 例 :ermao@lost-desktop:~$ alias ...
- 【Spring】Redis的两个典型应用场景--good
原创 BOOT Redis简介 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结构,例如hashes, lists, sets等,同时支持数据持久化. ...
- 增加oracle数据库最大连接数
这几天碰到系统不能登陆的情况,初步判断可能是数据库连接满了,(后来检查不是这个原因),做了一次增加数据库最大连接数操作.操作步骤如下 操作系统:Red Hat Enterprise Linux Ser ...