AFNetworking上传图片
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:@"XXXX" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { NSData *imageData = UIImageJPEGRepresentation(_userImg, 1); // 上传图片,以文件流的格式 name 对应网站上[upload.php中]处理文件的[字段"file"] fileName 不能为空且必须带后缀,如:test.jpg [formData appendPartWithFileData:imageData name:@"photo" fileName:@"XXXX.jpg" mimeType:@"image/jpeg"]; } success:^(AFHTTPRequestOperation *operation, id responseObject) { [_activityView stopAnimating]; if ([[responseObject objectForKey:@"success"] intValue] == YES) { [self makeToast:@"头像修改成功!" duration:2.0 position:@"bottom"]; [self saveUserInfo:[responseObject objectForKey:@"data"] key:@"avatar"]; }else{ id data = [responseObject objectForKey:@"data"]; [self makeToast:[data objectForKey:@"message"] duration:2.0 position:@"bottom"]; } } failure:^(AFHTTPRequestOperation *operation, NSError *error) { [_activityView stopAnimating]; [self makeToast:@"网络不给力!" duration:2.0 position:@"bottom"]; }];
特别注意注释行!
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-11-03 12:32:51