afnetworking 2.0 图片上传

2016-09-06  本文已影响35人  jadn

BaseDataService.h

typedefvoid(^RequestFinishBlock)(idresult);

+ (NSDictionary*)upImgRequestWithURL:(NSString*)url

params:(NSMutableDictionary*)params

image:(UIImage*)image

completeBlock:(RequestFinishBlock)block;

BaseDataService.m

#import "AFNetworking.h"

#define BASE_URL @"http://www.109.com/"

+ (NSDictionary*)upImgRequestWithURL:(NSString*)urlstring

params:(NSMutableDictionary*)params

image:(UIImage*)image

completeBlock:(RequestFinishBlock)block

{

//认证信息

NSString*accessToken =@"109";

//拼接URL

urlstring = [BASE_URLstringByAppendingFormat:@"%@?token=%@",urlstring,accessToken];

NSURL*url = [NSURLURLWithString:urlstring];

NSLog(@"%@",url);

__blockNSDictionary*result;

AFHTTPRequestOperationManager*manager = [[AFHTTPRequestOperationManageralloc]initWithBaseURL:url];

__blockNSData*imageData =UIImageJPEGRepresentation(image,0.5);

AFHTTPRequestOperation*op = [managerPOST:@""parameters:paramsconstructingBodyWithBlock:^(id formData) {

[formDataappendPartWithFileData:imageDataname:@"appImage"fileName:@"photo.jpg"mimeType:@"image/jpeg"];

}success:^(AFHTTPRequestOperation*operation,idresponseObject) {

//NSLog(@"Success: %@ ***** %@", operation.responseString, responseObject);

if(block !=nil) {

block(responseObject);

}

}failure:^(AFHTTPRequestOperation*operation,NSError*error) {

//NSLog(@"Error: %@ ***** %@", operation.responseString, error);

UIAlertView*alertView = [[UIAlertViewalloc]initWithTitle:@"请求失败,重新尝试!"

message:[errorlocalizedDescription]

delegate:nil

cancelButtonTitle:@"Ok"

otherButtonTitles:nil];

[alertViewshow];

}];

[opstart];

returnresult;

}

post.m:

NSMutableDictionary*params = [NSMutableDictionarydictionaryWithObjectsAndKeys:@"111",@"id",@"10",@"row",nil];

[BaseDataServiceupImgRequestWithURL:@""params:paramsimage:_imgView.imagecompleteBlock:^(idresult) {

NSMutableArray*statues = [resultobjectForKey:@"list"];

NSLog(@"%@",statues);

}];

上一篇下一篇

猜你喜欢

热点阅读