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. 生日蛋糕—dfs

    Description 7月17日是Mr.W的生日,ACM-THU为此要制作一个体积为Nπ的M层生日蛋糕,每层都是一个圆柱体. 设从下往上数第i(1 <= i <= M)层蛋糕是半径为Ri ...

  2. LINUX下如何开启FTP服务器

    1.首先应开启linuxh环境下的FTP service,过程如下:   http://www.witech.com.cn/news/Article_Show.asp?ArticleID=48    ...

  3. 标准库中的-stack

    #include <sequence_concepts.h> __STL_BEGIN_NAMESPACE // Forward declarations of operators == a ...

  4. linux下locate为什么有时候某些文件

    这与locate命令的工作原理有关.他是通过查询数据库的方式查找文件的.并且数据库每天更新一次.你要找的文件可能没有更新到数据库中. 可以有两种选择: 第一,过一天后再查看一下,应该就会找到了. 第二 ...

  5. oracle 常用语法

    一.ORACLE的启动和关闭1.在单机环境下要想启动或关闭ORACLE系统必须首先切换到ORACLE用户,如下su - oraclea.启动ORACLE系统oracle>svrmgrlSVRMG ...

  6. // 开始无限播放 ViewPager

    public class MainActivity extends Activity { private ViewPager vp;    private Handler handler = new ...

  7. OpenGL概述

    简介 状态机 glBegin()与glEnd() glFlush()与glFinish() OpenGL简介 OpenGL是图形硬件的一种软件接口.它被设计为硬件独立的接口,可用于多种不同硬件平台.O ...

  8. world machine, 输出lightmap

    一,输出黑白lightmap: 二,输出彩色lightmap: 需要注意的是:当输出黑白lightmap时,输出设备要用Height Output:当输出彩色lightmap时,输出设备要用Bitma ...

  9. mysql主从复制问题之主从两端binlog日志不同步解决方案

    主操作: 进入主的数据库查看状态: mysql> show master statusG; *************************** 1. row **************** ...

  10. SQLServer2012中用于记录数据操作时刻的附加字段使用datetime2(3)就可以了

    datetime2(3)精确到毫秒(听说),约等于2005时代的datetime类型.实际上后者是精确到3.33毫秒(也是听说). ) = GETDATE(); ) = GETDATE(); ) = ...