zoukankan      html  css  js  c++  java
  • 如何调用本地相册、相机

     
    1、判断系统是否支持相机
    [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]
    2、创建相册视图控制器对象
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    3、设置sourceType(资源类型)
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;//相机
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;//图片库
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;//保存的图片
    4、设置弹出动画(可选)
    imagePickerController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    5、设置是否允许编辑图片
    imagePickerController.allowsEditing = YES;
    6、设置代理(UIImagePickerControllerDelegate)
    imagePickerController.delegate = self;
    7、实现代理方法:获取图片
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
         [picker dismissViewControllerAnimated:YES completion:nil];
        //获取图片
         UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    //取消点击
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
    {
        [self dismissViewControllerAnimated:YES completion:^{}];
    }
    8、保存图片到相册
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);//image:要保存的图片UImage对象,
    - (void) image: (UIImage *) image
    didFinishSavingWithError: (NSError *) error
       contextInfo: (void *) contextInfo{
    //如果error==NULL,则表示图片保存成功
    }
     
    具体代码:
    - (void)viewDidLoad {
        [super viewDidLoad];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(10, 50, 80, 80);
        btn.backgroundColor = [UIColor redColor];
        [btn addTarget:self action:@selector(showpicker) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];   
    }
    -(void)showpicker{
        UIImagePickerController *imagepicker = [[UIImagePickerController alloc]init];
        imagepicker.sourceType = 2;//设置类型(0代表调用相册,1 代表弹出相机)
        imagepicker.allowsEditing = YES;//是否允许编辑,默认no,设置为yes时,点击图片会进入编辑界面(裁剪)
       
        imagepicker.delegate = self;//设置代理
        [self presentViewController:imagepicker animated:YES completion:nil];//弹出相册/相机
    }
    //选择图片后调用的代理方法
    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
        NSLog(@"%@",info);
        UIImage *image = info[@"UIImagePickerControllerEditedImage"];//获取图片
        UIImageView *imageview = [[UIImageView alloc]initWithImage:image];
        imageview.frame = CGRectMake(40, 150, 100, 100);
        [self.view addSubview:imageview]; 
        [self dismissViewControllerAnimated:YES completion:nil];//消失imagepicker
    }
  • 相关阅读:
    【胡策篇】题目
    【学术篇】luogu3768 简单的数学题(纯口胡无代码)
    【学术篇】规律选手再次证明自己(舒老师的胡策题 T2 LX还在迷路)
    【模板篇】Link Cut Tree模板(指针)
    【学术篇】51nod 1238 最小公倍数之和
    【学术篇】2.28测试T2 线段 拓扑排序
    【学术篇】SPOJ FTOUR2 点分治
    【颓废篇】Py:从零开始的poj自动提交
    【学术篇】CF935E Fafa and Ancient Mathematics 树形dp
    安卓启动图去除顶部title和状态栏
  • 原文地址:https://www.cnblogs.com/wxzboke/p/5061635.html
Copyright © 2011-2022 走看看