图片上传

基于AFNetworking3.0 的常用图片上传

JKHttpClientTool 对AFHTTPSessionManager 进行封装防止内存泄露


#import#import "AFHTTPSessionManager.h"

@interface JKHttpClientTool : NSObject

+ (AFHTTPSessionManager *)sharedManager;

@end


#import "JKHttpClientTool.h"

@interface JKHttpClientTool ()

@end

static AFHTTPSessionManager *_manager;

@implementation JKHttpClientTool

+ (AFHTTPSessionManager *)sharedManager {

static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

_manager = [AFHTTPSessionManager manager];

_manager.requestSerializer = [AFHTTPRequestSerializer serializer];

_manager.responseSerializer = [AFJSONResponseSerializer serializer];

_manager.requestSerializer.timeoutInterval = 30;

AFJSONResponseSerializer *response = [AFJSONResponseSerializer serializer];

response.removesKeysWithNullValues = YES;

_manager.responseSerializer = response;

_manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html",@"text/plain", nil];

});

return _manager;

}

@end


JKHttpServiceManager 图片上传请求的封装类

图片上传_第1张图片

具体请求方法由于代码过多只显示了.h部分文件  .m 文件暂时就单图片上传部分

图片上传_第2张图片

具体使用如下

#pragma mark ==单图片上传==

- (void)uploadImageOne {

//接口地址随便更改

NSString *url = @"www.baidu.com";

//字典

NSDictionary *params = @{@"test":@"1"};

//图片data

UIImage *goodImage = [[UIImage alloc]init];//随便替换成什么图片

NSData *imageData = UIImageJPEGRepresentation(goodImage, 0.5);//压缩上传

[JKHttpServiceManager POST:url Params:params NSData:imageData key:@"goods" success:^(id responseObject, BOOL succe, NSDictionary *jsonDic) {

if (succe) {

NSLog(@"上传成功");

}

} failure:^(NSError *error) {

} animated:YES];

}

其他的2种方法在demo中,如果觉得不错麻烦给个star,如果觉得有什么缺漏,也麻烦联系我让我及时修复。

联系方式 QQ:1084356436   烦请备注(图片上传)!

你可能感兴趣的:(图片上传)