码迷,mamicode.com
首页 > Web开发 > 详细

调用系统的相机上传照片

时间:2016-08-18 17:15:13      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:

  • -(void)tapGestureRecognizer:(UITapGestureRecognizer *)sender {
    
        UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"选择照片" delegate: self cancelButtonTitle:@"取消" destructiveButtonTitle:@"相机" otherButtonTitles:@"本地相册", nil];
        [action showInView:self.view];
    }
    
    - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    
        if (buttonIndex == 2) {
            return;
        }
    
        //创建图片选择器
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        //设置图片选择属性
        imagePicker.allowsEditing = NO;
      
        if (buttonIndex == 0) {
           
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                //真机打开
                imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            }else {
            
                //模拟器打开
                NSLog(@"模拟器打开");
                return;
            }
            
        }else {
            //相册
            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        }
        //进去选择器
        [self presentViewController:imagePicker animated:YES completion:nil];
        self.modalPresentationStyle = UIModalPresentationOverCurrentContext;
        
    }
    
    #pragma mark - UIImagePickerControllerDelegate
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
    
        NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
        
        //当选择的类型是图片
        if ([type isEqualToString:@"public.image"])
        {
            //先把图片转成NSData
            UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
            NSData *data;
            if (UIImagePNGRepresentation(image) == nil)
            {
                data = UIImageJPEGRepresentation(image, 1.0);
            }
            else
            {
                data = UIImagePNGRepresentation(image);
            }
            
            self.headImg.image = image;
            
            //图片保存的路径
            //这里将图片放在沙盒的documents文件夹中
            NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
            
            //文件管理器
            NSFileManager *fileManager = [NSFileManager defaultManager];
            
            //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png
            [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];
            [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];
            
            //得到选择后沙盒中图片的完整路径
            self.filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath,  @"/image.png"];
            
            //关闭相册界面
            [picker dismissViewControllerAnimated:YES completion:nil];
            
        
        //[self saveChangeData];
        }
        
    }
    //#pragma mark - delegate
    //- (void)saveChangeData {
    
        //NSData *imagData =UIImagePNGRepresentation(self.headImg.image);
        //NSString *imageStr = [[NSString alloc] initWithData:imagData encoding:NSUTF8StringEncoding];
        //NSLog(@"0------%@",imageStr);
        //[DataManager getInstance].user.head_image = imageStr;
    //}

     

调用系统的相机上传照片

标签:

原文地址:http://www.cnblogs.com/ningmengcao-ios/p/5784607.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!