技术内容:分别读取相册以及调取相机,将图片显示到imageView上

布局:

1.创建imageView 和 button 并为button一个关联pickerImage的事件

<div style="text-align: left;"><span style="font-family: Helvetica; -webkit-text-stroke-width: initial;">    self.aImageView = [[UIImageView alloc]initWithFrame:CGRectMake(60, 100, 200, 200)];</span></div>    self.aImageView.backgroundColor = [UIColor redColor];
    self.aImageView.userInteractionEnabled = YES;

    self.aButton = [[UIButton alloc]initWithFrame:CGRectMake(98, 350, 125, 25)];
    self.aButton.backgroundColor = [UIColor blueColor];
    [self.aButton addTarget:self action:@selector(pickerImage:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.aButton setTitle:@"选择图像" forState:(UIControlStateNormal)];

    [self.view addSubview:self.aButton];
    [self.view addSubview:self.aImageView];
    [self.aButton release];
    [self.aImageView release];

    [self addTapGestureOnImageView];

2.因为有的场景需要直接点击图片更换别的图片,所以在imageView上添加轻拍动作

- (void)addTapGestureOnImageView{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(pickerImage:)];
    [self.aImageView addGestureRecognizer:tap];
    [tap release];
}

3.实现轻拍动作中方法

- (void)pickerImage:(UIButton *)button{
    //添加ActionSheet控件,提示选项框,调出相机或拍摄图片
    //第一个参数:是行为列表的标题 一般为空
    //第二个参数:遵循代理
    //第三个参数:取消这个操作按钮上 显示的文字
    //第四个参数:destructive 破坏性的, 毁灭性的 自己理解吧 反正我写的是拍照,执行操作的意思
    //第五个参数:从相册选取图片
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"从相册选择图片", nil];
    //在当前界面显示actionSheet对象
    [actionSheet showInView:self.view];
    [actionSheet release];
}

4.实现action代理协议中的方法

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    switch (buttonIndex) {
        case 0:
            //调用系统相机,拍照
            [self pickerPictureFromCamera];
            break;
        case 1:
            [self pickerPictureFromPhotosAlbum];
        default:
            break;
    }
}

4.1从摄像头获取图片

- (void)pickerPictureFromCamera{

    //判断前摄像头是否可用,如果不可用的话,用后摄像头。如果后摄像头也不可用的话用手机图片库
    //判断前置摄像头是否可用
    if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront]) {
        NSLog(@"用前置摄像头");

        self.imagePC = [[UIImagePickerController alloc]init];
        self.imagePC.cameraDevice = UIImagePickerControllerCameraDeviceFront;
        [self.imagePC release];
        //判断后置摄像头是否可用
    }else if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]){
        NSLog(@"用后置摄像头");

        self.imagePC = [[UIImagePickerController alloc]init];
        self.imagePC.cameraDevice = UIImagePickerControllerCameraDeviceRear;
        [self.imagePC release];
        //两者都不行的话,从手机相册调取照片
    }else{
        UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"调用摄像头失败" message:@"请从手机相册中选取照片" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alertView show];
        [alertView release];
    }

    //初始化图片控制器对象
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    //sourceType资源样式
    //设置图片选择器选择图片的样式
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    //设置图片是否允许编辑
    imagePicker.allowsEditing = YES;
    //设置图片选择器代理对象为这个视图控制器
    imagePicker.delegate = self;
    //把相机推出来 模态
    [self presentViewController:imagePicker animated:YES completion:nil];
    //释放
    [imagePicker release];

}

4.2从手机的图片库获取图片

- (void)pickerPictureFromPhotosAlbum{
    //初始化图片控制器对象
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc]init];
    //sourceType资源样式
    //设置图片选择器选择图片的样式
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    //设置图片是否允许编辑
    imagePicker.allowsEditing = YES;
    //设置图片选择器代理对象为这个视图控制器
    imagePicker.delegate = self;
    //把选择控制器推出来 模态
    [self presentViewController:imagePicker animated:YES completion:nil];
    //释放
    [imagePicker release];
}

5.将选取好的照片保存到详情页的方法

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    //以相册作为字典,从中取出照片
    self.aImageView.image = [info objectForKey:UIImagePickerControllerEditedImage];
    //把选取框模态回去
    [self dismissViewControllerAnimated:YES completion:nil];
}

测试效果:(由于mac端虚拟机无前后摄像头所以直接跳转相册选取)

iOS中 读取相册,调用系统相机 技术分享的更多相关文章

  1. iOS中 UIWebView加载网络数据 技术分享

    直奔核心: #import "TechnologyDetailViewController.h" #define kScreenWidth [UIScreen mainScreen ...

  2. iOS开发 调用系统相机和相册 分类: ios技术 2015-03-30 15:52 65人阅读 评论(0) 收藏

     调用系统相机和相册 (iPad,iPhone) 打开相机:(iPad,iPhone) //先设定sourceType为相机,然后判断相机是否可用(ipod)没相机,不可用将sourceType设定为 ...

  3. iOS开发 调用系统相机和相册

    调用系统相机和相册 (iPad,iPhone)打开相机:(iPad,iPhone)//先设定sourceType为相机,然后判断相机是否可用(ipod)没相机,不可用将sourceType设定为相片库 ...

  4. Android7.0调用系统相机拍照、读取系统相册照片+CropImageView剪裁照片

    Android手机拍照.剪裁,并非那么简单 简书地址:[我的简书–T9的第三个三角] 前言 项目中,基本都有用户自定义头像或自定义背景的功能,实现方法一般都是调用系统相机–拍照,或者系统相册–选择照片 ...

  5. Android调用系统相机和相册并解决data为空,OOM,图片角度不对的问题

    最近公司项目用到手机拍照的问题,好不容易在网上copy了一些代码,但是运行起来一大堆bug,先是三星手机上运行程序直接崩掉,debug了一下原来是onActivityResult中data返回为空,找 ...

  6. ios中从相册:相机中获取图片信息

    ios中从相册/相机中获取图片信息 从相册中获取图片的信息 UIImagePickerController *imgPickView = [[UIImagePickerController alloc ...

  7. Android 调用系统相机拍照保存以及调用系统相册的方法

    系统已经有的东西,如果我们没有新的需求的话,直接调用是最直接的.下面讲讲调用系统相机拍照并保存图片和如何调用系统相册的方法. 首先看看调用系统相机的核心方法: Intent camera = new ...

  8. 【踩坑速记】MIUI系统BUG,调用系统相机拍照可能会带给你的一系列坑,将拍照适配方案进行到底!

    一.写在前面 前几天也是分享了一些学习必备干货(还没关注的,赶紧入坑:传送门),也好久没有与大家探讨技术方案了,心里也是挺痒痒的,这不,一有点闲暇之时,就迫不及待把最近测出来的坑分享给大家. 提起An ...

  9. ios中摄像头/相册获取图片压缩图片上传服务器方法总结

    本文章介绍了关于ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结,有需要了解的同学可以参考一下下.     这几天在搞iphone上面一个应用的开发,里面有需要摄像头/相册编程和图片上传的问 ...

随机推荐

  1. phpstorm查看类的继承关系

    在看一些框架源码时,有些类有很多的继承或者接口,有一款神奇的帮助很重要 选中一个类文件,右键,选择diagrams->show diagrams 即可得到类的继承关系,如上右图 使用函数 fun ...

  2. 混合式应用开发之Cordova+vue(1)

    一.Cordova创建应用 cordova create oneApp Cordova创建应用出错 Cordova安装时不能使用cnpm 应该使用npm,cnpm虽然快但是后期出的错绝对比这省下来的时 ...

  3. ubuntu14.04 64位 安装H3C iNode客户端

    环境: OS: ubuntu14.04LTS 64位 iNode:  iNode2.40-R0162 for linux(iNode只有32位的,而且是很久以前的版本) 安装方法: 第一种: 主要参考 ...

  4. Django中过期@cache_page中缓存的views数据

    django的缓存系统中,cache_page 这个装饰器非常好用,只要添加一个装饰器就可以缓存views的响应内容,但是django没有提供过期这个views缓存数据的功能. @cache_page ...

  5. [Gradle系列]Gradle打包apk多版本,多渠道,多环境,多功能,多模块随心所欲

    Tamic: http://blog.csdn.net/sk719887916/article/details/53411771 开始 上篇Gradle发布Module(Maven)到jcenter, ...

  6. Lua热更新时正确设置文件名

    Lua热更新时正确设置文件名(金庆的专栏 2016.12)Lua热更新模块见:https://github.com/jinq0123/hotfix其中使用 load(chunk) 来加载更新后的内容, ...

  7. Angular2的input和output(原先的properties和events)

    angular2学习笔记 本文地址:http://blog.csdn.net/sushengmiyan 本文作者:苏生米沿 文章来源:http://blog.ng-book.com/angular-2 ...

  8. Programming In Scala笔记-第十六章、Scala中的List

    本章主要分析Scala中List的用法,List上可进行的操作,以及需要注意的地方. 一.List字面量 首先看几个List的示例. val fruit = List("apples&quo ...

  9. 28 自定义View侧滑栏

    ScrollMenuView.java package com.qf.sxy.customview03.widget; import android.content.Context; import a ...

  10. 微信开发之使用java获取签名signature(贴源码,附工程)

    一.前言 微信接口调用验证最终需要用到的三个参数noncestr.timestamp.signature: 接下来将会给出获取这三个参数的详细代码 本文的环境eclipse + maven 本文使用到 ...