美文网首页
afnetworking 2.0 图片上传

afnetworking 2.0 图片上传

作者: jadn | 来源:发表于2016-09-06 11:17 被阅读35次

    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);

    }];

    相关文章

      网友评论

          本文标题:afnetworking 2.0 图片上传

          本文链接:https://www.haomeiwen.com/subject/xzjdettx.html