iOS gif图片上传(AFNetworking封装)

1.解析asset

PHImageRequestOptions*options = [PHImageRequestOptionsnew];

options.resizeMode=PHImageRequestOptionsResizeModeFast;

options.synchronous=YES;

PHCachingImageManager*imageManager = [[PHCachingImageManageralloc]init];

[imageManagerrequestImageDataForAsset:asset

options:options

resultHandler:^(NSData*_NullableimageData,NSString*_NullabledataUTI,UIImageOrientationorientation,NSDictionary*_Nullableinfo) {

NSLog(@"dataUTI:%@",dataUTI);

//gif图片

if([dataUTIisEqualToString:(__bridgeNSString*)kUTTypeGIF]) {

BOOLdownloadFinined = (![[infoobjectForKey:PHImageCancelledKey]boolValue] && ![infoobjectForKey:PHImageErrorKey]);

if(downloadFinined && imageData) {

NSString*method =@"user/uploadImage";

NSString*fileType =@"image/gif";

[JSONPost   postUploadWithUrl:nil name:@"image" fileData:imageData fileType:fileType method:@"upload/image" param:@{@"method":method}success:^(idresponseObject) {

idjsonObject = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];

}fail:^{

//上传失败

}];

}

}

else{

//其他格式的图片,直接请求压缩后的图片

}

}];


2.AFNetworking封装的上传方法

+ (void)postUploadWithUrl:(NSString*)urlStr name:(NSString*)name fileData:(NSData*)fileData fileType:(NSString*)fileTye method:(NSString*)method param:(NSDictionary*)param success:(void(^)(idresponseObject))success fail:(void(^)())fail

{

AFHTTPSessionManager*manager = [AFHTTPSessionManagermanager];

manager.responseSerializer= [AFHTTPResponseSerializerserializer];

manager.requestSerializer.timeoutInterval= 20.0f;

NSMutableDictionary*paramters = [NSMutableDictionarydictionaryWithDictionary:@{}]; //接口需要的参数

[managerPOST:STR_UTF_8(urlStr)parameters:paramtersconstructingBodyWithBlock:^(id_NonnullformData) {

//要上传保存在服务器中的名称

//使用时间来作为文件名2014-04-30 14:20:57.gif

//让不同的用户信息,保存在不同目录中

NSDateFormatter*formatter = [[NSDateFormatteralloc]init];

//设置日期格式

formatter.dateFormat=@"yyyy-MM-dd HH:mm:ss";

NSString*fileName = [formatterstringFromDate:[NSDatedate]];

fileName = [NSStringstringWithFormat:@"%@.gif",fileName];

//fileTye @"image/gif"

[formDataappendPartWithFileData:fileDataname:namefileName:fileNamemimeType:fileTye];

}progress:nilsuccess:^(NSURLSessionDataTask*_Nonnulltask,id_NullableresponseObject) {

if(success) {

success(responseObject);

}

}failure:^(NSURLSessionDataTask*_Nullabletask,NSError*_Nonnullerror) {

 //网络请求失败,请稍候再试。

if(fail) {

fail();

}

}];

}

你可能感兴趣的:(iOS gif图片上传(AFNetworking封装))