AFNetwork上传图片


[cpp]  view plain copy
  1. AFNetwork是一个轻量级的网络请求api类库。是以NSURLConnection, NSOperation和其他方法为基础的。  
  2.   
  3. 下面这个例子是用来处理json请求的:  
  4. NSURL *url = [NSURL URLWithString:@"https://alpha-api.app.net/stream/0/posts/stream/global"];  
  5. NSURLRequest *request = [NSURLRequest requestWithURL:url];  
  6. AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {  
  7.     NSLog(@"App.net Global Stream: %@", JSON);  
  8. } failure:nil];  
  9. [operation start];  
  10.   
  11.   
  12. 使用方法:  
  13. 1.下载AFNetwork,点击下载  
  14. 2.将文件夹名称为AFNetworking拖入到你的工程项目即可  
  15.   
  16. 常见问题:  
  17. 1. AFNetworking作用都有哪些?  
  18. NSURLConnection提供了+sendAsynchronousRequest:queue:completionHandler:和+sendAsynchronousRequest:queue:completionHandler: ,但是AFNetworking提供了更好的功能  
  19. *AFURLConnectionOperation和它的子类继承NSOperation的,允许请求被取消,暂停/恢复和由NSOperationQueue进行管理。  
  20. *AFURLConnectionOperation也可以让你轻松得完成上传和下载,处理验证,监控上传和下载进度,控制的缓存。  
  21. *AFHTTPRequestOperation和它得子类可以基于http状态和内容列下来区分是否成功请求了  
  22. *AFNetworking可以将远程媒体数据类型(NSData)转化为可用的格式,比如如JSON,XML,图像和plist。  
  23. *AFHTTPClient提供了一个方便的网络交互接口,包括默认头,身份验证,是否连接到网络,批量处理操作,查询字符串参数序列化,已经多种表单请求  
  24. *的UIImageView+ AFNetworking增加了一个方便的方法来异步加载图像。  
  25.   
  26. 2. AFNetworking是否支持缓存?  
  27. 可以,NSURLCache及其子类提供了很多高级接口用于处理缓存  
  28. 如果你想将缓存存储再磁盘,推荐使用SDURLCache  
  29.   
  30. 3.如何使用AFNetworking上传一个文件?  
  31. NSData *imageData = UIImagePNGRepresentation(image);  
  32. NSURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {  
  33.   [formData appendPartWithFileData:imageData mimeType:@"image/png" name:@"avatar"];  
  34. }];  
  35.   
  36.   
  37. 4.如何使用AFNetworking下载一个文件?  
  38. 先创建一个AFURLConnectionOperation对象,然后再使用它的属性outputStream进行处理  
  39. operation.outputStream = [NSOutputStream outputStreamToFileAtPath:@"download.zip" append:NO];  
  40.   
  41. 5.如何解决:SystemConfiguration framework not found in project  
  42. 请导入:  
  43. #import <SystemConfiguration/SystemConfiguration.h>  
  44. #import <MobileCoreServices/MobileCoreServices.h>  
  45.   
  46. 6.当应用程序退出时,如何保持持续的请求?  
  47. AFURLConnectionOperation有一个叫setShouldExecuteAsBackgroundTaskWithExpirationHandler:的方法用于处理在应用程序进入后台后,进行持续的请求  
  48. [self setShouldExecuteAsBackgroundTaskWithExpirationHandler:^{  
  49. }];  
  50.   
  51.   
  52.   
  53. 一些实例:  
  54. 1.XML 请求  
  55. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.flickr.com/services/rest/?method=flickr.groups.browse&api_key=b6300e17ad3c506e706cb0072175d047&cat_id=34427469792%40N01&format=rest"]];  
  56. AFXMLRequestOperation *operation = [AFXMLRequestOperation XMLParserRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, NSXMLParser *XMLParser) {  
  57.   XMLParser.delegate = self;  
  58.   [XMLParser parse];  
  59. } failure:nil];  
  60. [operation start];  
  61.   
  62.   
  63. 2.图片请求:  
  64. UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];  
  65. [imageView setImageWithURL:[NSURL URLWithString:@"http://i.imgur.com/r4uwx.jpg"] placeholderImage:[UIImage imageNamed:@"placeholder-avatar"]];  
  66.   
  67. 3.图片上传处理,监测上传状态:  
  68. didiwei  17:28:57  
  69. NSURL *url = [NSURL URLWithString:@"http://api-base-url.com"];  
  70. AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];  
  71. NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar.jpg"], 0.5);  
  72. NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {  
  73.     [formData appendPartWithFileData:imageData name:@"avatar" fileName:@"avatar.jpg" mimeType:@"image/jpeg"];  
  74. }];  
  75. AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];  
  76. [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {  
  77.     NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);  
  78. }];  
  79. [operation start];  
  80.   
  81.   
  82. 4.在线流媒体请求  
  83. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://localhost:8080/encode"]];  
  84. AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];  
  85. operation.inputStream = [NSInputStream inputStreamWithFileAtPath:[[NSBundle mainBundle] pathForResource:@"large-image" ofType:@"tiff"]];  
  86. operation.outputStream = [NSOutputStream outputStreamToMemory];  
  87. [operation start];  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值