zoukankan      html  css  js  c++  java
  • 拍照或从相册选择图片并上传服务器代码demo

    #import "HMViewController.h"
    #import "AFNetworking.h"
    
    @interface HMViewController () <UINavigationControllerDelegate, UIImagePickerControllerDelegate,UIActionSheetDelegate>
    @property (weak, nonatomic) IBOutlet UIImageView *imageView;
    - (IBAction)upload;
    @end
    
    @implementation HMViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"请选择图片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", @"相册", nil];
        [sheet showInView:self.view.window];
    }
    
    #pragma mark - UIActionSheet
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
        // 设置代理
        ipc.delegate = self;
        
        switch (buttonIndex) {
            case 0: { // 拍照
                if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) return;
                ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
                break;
            }
            case 1: { // 相册
                if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) return;
                ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                break;
            }
            default:
                break;
        }
        
        // 显示控制器
        [self presentViewController:ipc animated:YES completion:nil];
    }
    
    #pragma mark - UIImagePickerControllerDelegate
    /**
     *  在选择完图片后调用
     *
     *  @param info   里面包含了图片信息
     */
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        // 销毁控制器
        [picker dismissViewControllerAnimated:YES completion:nil];
        
        // 获得图片
        UIImage *image = info[UIImagePickerControllerOriginalImage];
        
        // 显示图片
        self.imageView.image = image;
    }
    
    - (void)upload1
    {
        // 1.创建一个管理者
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
        
        // 2.封装参数(这个字典只能放非文件参数)
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"username"] = @"123";
        params[@"age"] = @20;
        params[@"pwd"] = @"456";
        params[@"height"] = @1.55;
        
        // 2.发送一个请求
        NSString *url = @"http://localhost:8080/MJServer/upload";
        [mgr POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            // 在发送请求之前会自动调用这个block
            // 需要在这个block中添加文件参数到formData中
            
            /**
             FileURL : 需要上传的文件的URL路径
             name : 服务器那边接收文件用的参数名
             fileName : (告诉服务器)所上传文件的文件名
             mimeType : 所上传文件的文件类型
             */
            NSURL *url = [[NSBundle mainBundle] URLForResource:@"itcast" withExtension:@"txt"];
            [formData appendPartWithFileURL:url name:@"file" fileName:@"test.txt" mimeType:@"text/plain" error:nil];
            
            /**
             FileData : 需要上传的文件的具体数据
             name : 服务器那边接收文件用的参数名
             fileName : (告诉服务器)所上传文件的文件名
             mimeType : 所上传文件的文件类型
             */
            //        UIImage *image = [UIImage imageNamed:@"minion_01"];
            //        NSData *fileData = UIImagePNGRepresentation(image);
            //        [formData appendPartWithFileData:fileData name:@"file" fileName:@"haha.png" mimeType:@"image/png"];
        } success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"上传成功");
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"上传失败");
        }];
    }
    
    - (IBAction)upload {
        if (self.imageView.image == nil) return;
        
        // 1.创建一个管理者
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
        
        // 2.封装参数(这个字典只能放非文件参数)
        NSMutableDictionary *params = [NSMutableDictionary dictionary];
        params[@"username"] = @"123";
        params[@"age"] = @20;
        params[@"pwd"] = @"456";
        params[@"height"] = @1.55;
        
        // 2.发送一个请求
        NSString *url = @"http://192.168.15.172:8080/MJServer/upload";
        [mgr POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            NSData *fileData = UIImageJPEGRepresentation(self.imageView.image, 1.0);
            [formData appendPartWithFileData:fileData name:@"file" fileName:@"haha.jpg" mimeType:@"image/jpeg"];
    
            // 不是用这个方法来设置文件参数
    //        [formData appendPartWithFormData:fileData name:@"file"];
        } success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@"上传成功");
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@"上传失败");
        }];
        
        
    @end
  • 相关阅读:
    IA__gdk_drawable_get_size: assertion 'GDK_IS_DRAWABLE (drawable)' failed
    宿主机系统 Deepin 15.4,解决 Virtualbox 5.1 中 XP虚拟机无法使用 USB设备(如:U盘、罗技优联接收器等)的问题
    Deepin安装Virtualbox扩展包出现与gksu-run-helper通信失败的解决
    docker 学习资料收集
    从编程语言的角度看中医的【藏像】理论
    从程序员视角和编程语言角度看【中医】:一种生命健康编程语言
    使用微服务架构思想,设计部署API代理网关和OAuth2.0授权认证框架
    在Office应用中打开WPF窗体并且让子窗体显示在Office应用上
    彻底关闭Excle进程的几个方法
    70后.net老猿,尚能饭否?
  • 原文地址:https://www.cnblogs.com/ZMiOS/p/5193894.html
Copyright © 2011-2022 走看看