1.新建viewController 拖入一个Button,添加点击事件,使用代理方法

<UIActionSheetDelegate,UIImagePickerControllerDelegate>

2.代码如下- (IBAction)DoChoose:(id)sender {


UIActionSheet *sheet;
//检查是否有摄像头功能
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { sheet = [[UIActionSheet alloc]
initWithTitle:@"选择"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:@"取消"
otherButtonTitles:@"拍照",@"从相册选择", nil];
}
else
{
sheet = [[UIActionSheet alloc]
initWithTitle:@"选择"
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:@"取消"
otherButtonTitles:@"从相册选择", nil];
}
sheet.tag=;
[sheet showInView:self.view];
}

//代理方法,启用拍照或使用相册功能
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (actionSheet.tag == ) {
NSUInteger sourceType = ;
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
switch (buttonIndex) {
case :
return;
case :
sourceType = UIImagePickerControllerSourceTypeCamera;
break;
case :
sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
break;
default:
break;
}
}
else
{
if (buttonIndex ==) {
return;
}
else
{
sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
}
} UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; imagePickerController.delegate = self; imagePickerController.allowsEditing = YES; imagePickerController.sourceType = sourceType; [self presentViewController:imagePickerController animated:YES completion:^{}];
}
}

//返回的图片数据
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
//返回到主界面中
[picker dismissViewControllerAnimated:YES completion:^{}];

//获取返回的图片
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

//压缩图片
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);

//沙盒,准备保存的图片地址和图片名称
NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"x.jpg"];

//将图片写入文件中
[imageData writeToFile:fullPath atomically:NO];

//通过路径获取到保存的图片,可以在主界面上的image进行预览
UIImage *saveImage = [[UIImage alloc]initWithContentsOfFile:fullPath]; [self.img1 setImage:saveImage]; //将图片变为Base64格式,可以将数据通过接口传给后台
    NSData *data = UIImageJPEGRepresentation(saveImage, 1.0f);
    NSString *baseString = [data base64Encoding];
}
//关闭拍照窗口
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:^{}];
}

iOS 调用拍照、选择本地相册、上传功能---未完善。的更多相关文章

  1. 选择本地文件上传控件 input标签

    当要通过控件来选择本地文件上传的时候的一种方式 <input type="file" id="input-file"/> 注意 type类型一定要是 ...

  2. IOS UIImagePickerController(拍照或者读取相册)

      UIImagePickerController ● 使用UIImagePickerController就可以进行拍照或者读取相册 ● 通过sourceType属性来决定拍照还是读取相册 ➢ UII ...

  3. IOS 调用系统照相机和相册

    /** *  调用照相机 */ - (void)openCamera { UIImagePickerController *picker = [[UIImagePickerController all ...

  4. iOS视频压缩存储至本地并上传至服务器-b

    最近做了一个项目,我把其中的核心功能拿出来和大家分享一下,重点还是自己梳理一下. 这里关于视频转码存储我整理了两个方法,这两个方法都是针对相册内视频进行处理的. 1.该方法没有对视频进行压缩,只是将视 ...

  5. iOS视频压缩存储至本地并上传至服务器

    最近做了一个项目,我把其中的核心功能拿出来和大家分享一下,重点还是自己梳理一下. 这里关于视频转码存储我整理了两个方法,这两个方法都是针对相册内视频进行处理的. 1.该方法没有对视频进行压缩,只是将视 ...

  6. Android WebView选择本地文件上传

    This sample demonstrate android webview choose file to upload. I just implement the client code ,the ...

  7. swift 实现拍照 选择相册

    //点击按钮的方法 func photos() { self.showBottomAlert() } /// 屏幕底部弹出的Alert func showBottomAlert(){ let aler ...

  8. Android 拍照或者从相册获取图片的实现

    我们常常会用到上传头像,或者发帖子的时候选择本地图片上传的功能.这个很常见 今天因为app的需求我研究了下.现在分享下. 其实不论是通过拍照还是从相册选取都会用到Intent 这是系统提供给我们用来调 ...

  9. Android中通过访问本地相册或者相机设置用户头像

    目前几乎所有的APP在用户注册时都会有设置头像的需求,大致分为三种情况: (1)通过获取本地相册的图片,经过裁剪后作为头像. (2)通过启动手机相机,现拍图片然后裁剪作为头像. (3)在APP中添加一 ...

随机推荐

  1. My first Scratch small game

    My first Scratch small game:https://scratch.mit.edu/projects/62700370/ PC or Mac only. Browser & ...

  2. Python 利用pytesser模块识别图像文字

    使用的是python的pytesser模块,原先想做的是图片中文识别,搞了一段时间了,在中文的识别上还是有很多问题,这里做记录分享. pytesser,OCR in Python using the ...

  3. 重新安装配置ubuntu的引导菜单

     查看分区挂在情况,找到ubuntu所在分区(boot)$sudo fdisk   -l   卸载isodevice镜像设备所在盘分区(boot) $sudo  umount  -l  /isodev ...

  4. tomcat内存溢出 PermGen space

    1. java.lang.OutOfMemoryError: PermGen space  ---- PermGen space溢出.  PermGen space的全称是Permanent Gene ...

  5. 建立一个简单的SpringMVC程序

    首先,所建立的程序是一个web程序,所以在web.xml文件中进行如下的配置: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 ...

  6. CentOS 6.5 安装 Python3

    1.安装环境 yum -y install gcc zlib-devel make 2.下载python版本 wget http://www.python.org/ftp/python/3.5.1/P ...

  7. 【原】CSS3的3D动画 ——3D旋转之骰子样式的钟表(2)下.md

    之前看到智能社主页的那个骰子样式的钟表,最近研究了一下,虽然没有仔细看他是怎么做的,但是学了css3的动画之后想自己尝试着写一下,用到的原理可能和智能社网站的不太一样,我自己主要用到了css3和js. ...

  8. Linq系列

    LINQ 图解 Linq中的Select——投影 Linq学习资源 Expert C# 5.0中的Linq部分

  9. connectionString加密

    首先是加密,解密类. using System; using System.Collections.Generic; using System.IO; using System.Linq; using ...

  10. RxJava_ _学了下RxJava

    之前就知道有RxJava这玩意,知道这玩意很屌,不过也就止于看看标题,看几段介绍的程度(懒癌害人不浅).这周心血来潮,抽空把之前收藏的 扔物线 大神写的RxJava入门文章看了. http://gan ...