|
字段 |
必填 |
类型 |
说明 |
|
userid |
true |
int |
用户id |
|
img |
false |
file |
上传头像(最大200KB)A |
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSData *imageData = UIImagePNGRepresentation(image);
[formData appendPartWithFileData:imageData name:@"img" fileName:@"23456789.jpg" mimeType:@"image/jpeg"];
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);"><span style="white-space:pre"> </span>//@"img" <span style="line-height: normal; font-family: 'Heiti SC Light';">服务器上的</span>img <span style="line-height: normal; font-family: 'Heiti SC Light';">字段</span> <span style="line-height: normal; font-family: 'Heiti SC Light';">容易出错</span></p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; line-height: normal; font-family: Menlo; color: rgb(29, 148, 33);"><span style="font-variant-ligatures: no-common-ligatures; color: #000000"> </span>//@"23456789.jpg" <span style="line-height: normal; font-family: 'Heiti SC Light';">图片名</span> <span style="line-height: normal; font-family: 'Heiti SC Light';">随便填</span></p>
//@"image/jpeg" 图片类型
} success:^(AFHTTPRequestOperation * _Nonnull operation, id responseObject) {
HeadImage *headImage = [HeadImage objectWithJSONData:responseObject];
success(headImage);
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * error) {
NSLog(@"%@",error);
failure(error);
}];
本文介绍了一种使用Objective-C进行iOS应用头像上传的方法。通过AFNetworking库的AFHTTPRequestOperationManager来实现文件上传功能,详细展示了如何设置请求参数、构造表单数据及处理响应等关键步骤。
628

被折叠的 条评论
为什么被折叠?



