首页 > 代码库 > IOS上传头像
IOS上传头像
//1 ASI post传图片
NSURL *url=[NSURL URLWithString:[BASEURL stringByAppendingString:@"/user/post_face.php"]];
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:url];
request.delegate=self;
request.timeOutSeconds=120;
request.requestMethod=@"POST";
NSUserDefaults *defaluts=[NSUserDefaults standardUserDefaults];
NSData *data=http://www.mamicode.com/UIImageJPEGRepresentation(editedImage, 1);
[request setData:data withFileName:@"aa.png" andContentType:@"image/png" forKey:@"user_img"];
[request setPostValue:[defaluts objectForKey:@"token"] forKey:@"token"];
[request startAsynchronous];
//2 AFNetworking 传图片
//1.上传图片
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
// NSMutableDictionary *dic=[[NSMutableDictionary alloc] init];
// [dic setObject:@"MemberCenter" forKey:@"m"];
// [dic setObject:@"uploadPhoto" forKey:@"a"];
NSDictionary *dic = @{@"m": @"Appraise",@"a":@"doReview"};
// 加载图片
NSArray *array = @[@"000",@"1"];
[manager POST:[NSString stringWithFormat:@"http://app.sssos.cn/index.php?"] parameters:dic constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
for (int i = 0; i<array.count; i++)
{
UIImage *image=[UIImage imageNamed:[array objectAtIndex:i]];
NSData *data = http://www.mamicode.com/UIImagePNGRepresentation(image);
[formData appendPartWithFileData:data name:@"Filedata[]" fileName:[NSString stringWithFormat:@"%@.png",[array objectAtIndex:i]] mimeType:@"image/png"];
}
} success:^(AFHTTPRequestOperation *operation,id responseObject) {
NSLog(@"Success:%@",responseObject);
} failure:^(AFHTTPRequestOperation *operation,NSError *error) {
// NSLog(@"%@",operation.responseString);
NSLog(@"Error: %@", error);
}];