上传图片的方式(AFNetworking 3.0)

  1. AFNetworking 3.0版本


  2. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info  

  3. {  

  4.     NSLog(@"图片选中");  

  5.     //截取图片  

  6.     UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];  

  7.     NSData *imageData = UIImageJPEGRepresentation(image, 0.001);  

  8.     self.mv.portraitImageView.image = image;  

  9.       

  10.     AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];  

  11.     manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"text/plain", nil nil];  

  12.     // 参数  

  13.     NSMutableDictionary *parameter = [NSMutableDictionary dictionary];  

  14.     parameter[@"token"] = "param....";  

  15.     // 访问路径  

  16.     NSString *stringURL = [NSString stringWithFormat:@"%@%@",HOSTURL,kUploadAvatar];  

  17.       

  18.     [manager POST:stringURL parameters:parameter constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {  

  19.         // 上传文件  

  20.         NSDateFormatter *formatter = [[NSDateFormatter alloc] init];  

  21.         formatter.dateFormat = @"yyyyMMddHHmmss";  

  22.         NSString *str = [formatter stringFromDate:[NSDate date]];  

  23.         NSString *fileName = [NSString stringWithFormat:@"%@.jpg", str];  

  24.           

  25.         [formData appendPartWithFileData:imageData name:@"file" fileName:fileName mimeType:@"image/png"];  

  26.           

  27.     } success:^(AFHTTPRequestOperation *operation, id responseObject) {  

  28.         NSLog(@"上传成功");  

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

  30.         NSLog(@"上传错误");  

  31.     }];  

  32. }


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