首页 > 代码库 > AFHTTPRequestOperationManager注意点
AFHTTPRequestOperationManager注意点
AFHTTPRequestOperationManager注意点
ActionSheet在控制器view上,因为点击拍照时,会modal出另外一个控制器,当前的控制器view就会从窗口中移除。就会报ActionSheet不在窗口上。
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
{
UIActionSheet *sheet = [[UIActionSheetalloc]initWithTitle:@"图片上传"delegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"拍照",@"相册",nil];
[sheet showInView:self.view];
}
#pragma mark -实现UIActionSheetDelegate代理方法
- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
UIImagePickerController *ipc = [[UIImagePickerControlleralloc]init];
ipc.delegate= self;
switch (buttonIndex) {
case 0:
if (![UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])return;
ipc.sourceType= UIImagePickerControllerSourceTypeCamera;
break;
case 1:
if (![UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])return;
ipc.sourceType= UIImagePickerControllerSourceTypePhotoLibrary;
break;
default:
break;
}
[selfpresentViewController:ipcanimated:YEScompletion:nil];
}
{
UIActionSheet *sheet = [[UIActionSheetalloc]initWithTitle:@"图片上传"delegate:selfcancelButtonTitle:@"取消"destructiveButtonTitle:nilotherButtonTitles:@"拍照",@"相册",nil];
[sheet showInView:self.view];
}
#pragma mark -实现UIActionSheetDelegate代理方法
- (void)actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
UIImagePickerController *ipc = [[UIImagePickerControlleralloc]init];
ipc.delegate= self;
switch (buttonIndex) {
case 0:
if (![UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])return;
ipc.sourceType= UIImagePickerControllerSourceTypeCamera;
break;
case 1:
if (![UIImagePickerControllerisSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary])return;
ipc.sourceType= UIImagePickerControllerSourceTypePhotoLibrary;
break;
default:
break;
}
[selfpresentViewController:ipcanimated:YEScompletion:nil];
}
注意:
如果在switch的case中定义一个局部变量,需要用大括号括住。
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
// 关闭UIImagePickerController控制器
[selfdismissViewControllerAnimated:YEScompletion:nil];
NSLog(@"%@",info);
// 获取图片
UIImage *image = info[UIImagePickerControllerOriginalImage];
self.imageV.image= image;
}
- (IBAction)upLoad
{
// 创建一个管理者
AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManagermanager];
// 设置参数
NSMutableDictionary *params = [NSMutableDictionarydictionary];
params[@"username"] =@"123";
params[@"pwd"] =@"123";
// 设置URL
NSString *url =@"http://192.168.15.56:8080/MJServer/upload";
[manger POST:url parameters:paramsconstructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
{
// 创建一个管理者
AFHTTPRequestOperationManager *manger = [AFHTTPRequestOperationManagermanager];
// 设置参数
NSMutableDictionary *params = [NSMutableDictionarydictionary];
params[@"username"] =@"123";
params[@"pwd"] =@"123";
// 设置URL
NSString *url =@"http://192.168.15.56:8080/MJServer/upload";
[manger POST:url parameters:paramsconstructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
注意:
在发送请求之前会自动调用这个block
需要在这个block中添加文件参数到formData中
NSData *fileData =http://www.mamicode.com/UIImageJPEGRepresentation(self.imageV.image,1.0);
[formData appendPartWithFileData:fileDataname:@"file"fileName:@"image.png"mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation*operation, id responseObject) {
NSLog(@"上传成功");
} failure:^(AFHTTPRequestOperation*operation, NSError *error) {
NSLog(@"上传失败");
}];
}
需要在这个block中添加文件参数到formData中
NSData *fileData =http://www.mamicode.com/UIImageJPEGRepresentation(self.imageV.image,1.0);
[formData appendPartWithFileData:fileDataname:@"file"fileName:@"image.png"mimeType:@"image/png"];
} success:^(AFHTTPRequestOperation*operation, id responseObject) {
NSLog(@"上传成功");
} failure:^(AFHTTPRequestOperation*operation, NSError *error) {
NSLog(@"上传失败");
}];
}
注意:
1.保留照片的质量
NSData *fileData =http://www.mamicode.com/ UIImageJPEGRepresentation(self.imageV.image, 1.0);
2.AFN不能监听文件的上传进度,ASI可以监听文件的上传进度。
3.需要上传的文件的具体数据
FileData :需要上传的文件的具体数据
name : 服务器那边接收文件用的参数名
fileName : (告诉服务器)所上传文件的文件名
mimeType : 所上传文件的文件类型
name : 服务器那边接收文件用的参数名
fileName : (告诉服务器)所上传文件的文件名
mimeType : 所上传文件的文件类型
NSData*fileData =http://www.mamicode.com/ UIImageJPEGRepresentation(self.imageV.image,1.0);
[formData appendPartWithFileData:fileDataname:@"file"fileName:@"image.png"mimeType:@"image/png"];
[formData appendPartWithFileData:fileDataname:@"file"fileName:@"image.png"mimeType:@"image/png"];
4.需要上传的文件的URL路径
FileURL :需要上传的文件的URL路径
name : 服务器那边接收文件用的参数名
fileName : (告诉服务器)所上传文件的文件名
mimeType : 所上传文件的文件类型
NSURL*url = [[NSBundlemainBundle]URLForResource:@"itcast"withExtension:@"txt"];
[formData appendPartWithFileURL:urlname:@"file"fileName:@"test.txt"mimeType:@"text/plain"error:nil];
name : 服务器那边接收文件用的参数名
fileName : (告诉服务器)所上传文件的文件名
mimeType : 所上传文件的文件类型
NSURL*url = [[NSBundlemainBundle]URLForResource:@"itcast"withExtension:@"txt"];
[formData appendPartWithFileURL:urlname:@"file"fileName:@"test.txt"mimeType:@"text/plain"error:nil];
AFHTTPRequestOperationManager注意点
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。