AFNetworking                                                                  

1. AFNetworking对IOS支持情况

 

AFNetworking VersionMinimum iOS TargetMinimum OS X TargetNotes
2.x iOS 6 OS X 10.8 Xcode 5 is required. NSURLSession subspec requires iOS 7 or OS X 10.9.
1.x iOS 5 Mac OS X 10.7  
0.10.x iOS 4 Mac OS X 10.6  

 

2. 使用方法

使用CocoaPods进行安装使用,Podfile如下:

platform :ios, '7.0'
pod "AFNetworking", "~> 2.0"

3. 基本使用方法

1. 获取当前设备的网络状态

 

+ (void)netWorkStatus
{
   /**
    AFNetworkReachabilityStatusUnknown          = -1,  // 未知
    AFNetworkReachabilityStatusNotReachable     = 0,   // 无连接
    AFNetworkReachabilityStatusReachableViaWWAN = 1,   // 3G 花钱
    AFNetworkReachabilityStatusReachableViaWiFi = 2,   // WiFi
    */
   // 如果要检测网络状态的变化,必须用检测管理器的单例的startMonitoring
   [[AFNetworkReachabilityManager sharedManager] startMonitoring];
   
   // 检测网络连接的单例,网络变化时的回调方法
   [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
       NSLog(@"%ld", status);
   }];
}

 

2. 获取JSON格式数据

+ (void)JSONDataWithUrl:(NSString *)url success:(void (^)(id json))success fail:(void (^)())fail;
{
   AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
   
   NSDictionary *dict = @{@"format": @"json"};
   // 网络访问是异步的,回调是主线程的,因此程序员不用管在主线程更新UI的事情
   [manager GET:url parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
       if (success) {
           success(responseObject);
       }
   } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       NSLog(@"%@", error);
       if (fail) {
           fail();
       }
   }];
}

 

3. 获取XML数据

  1. + (void)XMLDataWithUrl:(NSString *)urlStr success:(void (^)(id xml))success fail:(void (^)())fail
    {
       AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
       
       // 返回的数据格式是XML
       manager.responseSerializer = [AFXMLParserResponseSerializer serializer];
       
       NSDictionary *dict = @{@"format": @"xml"};
       
       // 网络访问是异步的,回调是主线程的,因此程序员不用管在主线程更新UI的事情
       [manager GET:urlStr parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
           if (success) {
               success(responseObject);
           }
           
       } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
           NSLog(@"%@", error);
           if (fail) {
               fail();
           }
       }];
    }

     

 

4. POST方式提交数据(其实也能够获取数据)

+ (void)postJSONWithUrl:(NSString *)urlStr parameters:(id)parameters success:(void (^)(id responseObject))success fail:(void (^)())fail
{
   AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
   // 设置请求格式
   manager.requestSerializer = [AFJSONRequestSerializer serializer];
   // 设置返回格式
   manager.responseSerializer = [AFHTTPResponseSerializer serializer];
   [manager POST:urlStr parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSString *result = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
       if (success) {
           success(responseObject);
       }
   } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
       NSLog(@"%@", error);
       if (fail) {
           fail();
       }
   }];
   
}

 

 

5. 下载图片

 

   NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.scott-sherwood.com/wp-content/uploads/2013/01/scene.png"]];
   AFImageRequestOperation *operation = [AFImageRequestOperation imageRequestOperationWithRequest:request
                                                                             imageProcessingBlock:nil
                                                                                          success:^(NSURLRequest *request, NSHTTPURLResponse*response, UIImage *image) {
       self.backgroundImageView.image = image;
   } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error) {
       NSLog(@"Error %@",error);
   }];
   
   [operation start];

 

 

 

SDWebImage使用,图片加载和缓存

 

 

这个类库提供一个UIImageView类别以支持加载来自网络的远程图片。具有缓存管理、异步下载、同一个URL下载次数控制和优化等特征。
使用方法.

自动缓存时候缓存图片名字是以MD5进行加密的后的名字进行命名.(因为加密那堆字串是唯一的)

1.   ImageView显示图片时使用方法

UITableView使用UIImageView+WebCache类(基本应用,UIImageView的一个category)

前提#import导入UIImageView+WebCache.h文件,然后在tableview的cellForRowAtIndexPath:方法下:

 [imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
异步的方式,并且能够自动的缓存。

 

2. 使用SDWebImageManager类:可以进行一些异步加载的工作

SDWebImageManager *manager = [SDWebImageManager sharedManager];
UIImage *cachedImage = [manager imageWithURL:url]; // 将需要缓存的图片加载进来
if (cachedImage) {
     // 如果Cache命中,则直接利用缓存的图片进行有关操作
     // Use the cached image immediatly
} else {
     // 如果Cache没有命中,则去下载指定网络位置的图片,并且给出一个委托方法
     // Start an async download
    [manager downloadWithURL:url delegate:self];
}

 

然后需要实现委托

  1. // 当下载完成后,调用回调方法,使下载的图片显示
    - (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image {
       // Do something with the downloaded image
    }

3. 独立异步图像缓存

独立的异步图像缓存

SDImageCache类提供一个创建空缓存的实例,并用方法imageForKey:来寻找当前缓存。

 

UIImage *myCachedImage = [[SDImageCache sharedImageCache] imageFromKey:myCacheKey];
//存储一个图像到缓存是使用方法storeImage: forKey:
[[SDImageCache sharedImageCache] storeImage:myImage forKey:myCacheKey];

 

默认情况下,图像将被存储在内存缓存和磁盘缓存中。如果仅仅是想内存缓存中,要使用storeImage:forKey:toDisk:方法的第三个参数带一负值来替代。

 

posted @ 2015-09-16 20:35 山是水的故事 阅读(336) 评论(0) 推荐(0) 编辑
摘要: http://stackoverflow.com/questions/13463197/the-android-emulator-does-not-start-avdYou ran into the same problem that I did, outlined inthis issue. Fi... 阅读全文
posted @ 2014-10-28 21:46 山是水的故事 阅读(303) 评论(0) 推荐(0) 编辑
摘要: 使用Ctrl+`快捷键或者通过View->Show Console菜单打开命令行,粘贴如下代码:1importurllib.request,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path();... 阅读全文
posted @ 2014-10-26 21:35 山是水的故事 阅读(206) 评论(0) 推荐(0) 编辑
摘要: 1. 安装下载Sun Java包,然后解压文件 tar -zxvf xxx.tar.gz, 然后 mv jdk文件夹 /usr/local/jdk然后设置环境变量sudo gedit /etc/profile JAVA_HOME=/usr/local/jdk export JRE_HOME=... 阅读全文
posted @ 2014-10-26 16:43 山是水的故事 阅读(283) 评论(0) 推荐(0) 编辑
摘要: Givennnon-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histog... 阅读全文
posted @ 2014-09-07 16:53 山是水的故事 阅读(252) 评论(0) 推荐(0) 编辑
摘要: Container With Most WaterTotal Accepted:15862Total Submissions:50802My SubmissionsGivennnon-negative integersa1,a2, ...,an, where each represents a po... 阅读全文
posted @ 2014-09-07 15:22 山是水的故事 阅读(120) 评论(0) 推荐(0) 编辑
摘要: There areNgas stations along a circular route, where the amount of gas at stationiisgas[i].You have a car with an unlimited gas tank and it costscost[... 阅读全文
posted @ 2014-09-06 23:52 山是水的故事 阅读(303) 评论(0) 推荐(0) 编辑
摘要: 递归算法bool valid(vector &res, int r) { int nCol = res.size(); for(int i=0;i res, int &nCount) { if (l == n) { ... 阅读全文
posted @ 2014-09-06 22:27 山是水的故事 阅读(141) 评论(0) 推荐(0) 编辑
摘要: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the fol... 阅读全文
posted @ 2014-09-06 16:27 山是水的故事 阅读(185) 评论(0) 推荐(0) 编辑
摘要: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm sh... 阅读全文
posted @ 2014-09-05 16:38 山是水的故事 阅读(116) 评论(0) 推荐(0) 编辑
点击右上角即可分享
微信分享提示