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. Android 中Thread,Handler,Loop学习

    1.先看一下最简单的进度条示例 EG: package com.sxz.android.thread; import java.util.concurrent.atomic.AtomicBoolean ...

  2. NIC bonding

    Bonding is the same as port trunking. In the following I will use the word bonding because practical ...

  3. IE6 IE7 不支持 JSON

    最近发现ie6.7不支持json,解决方法:引入json2.js     <script type="text/javascript" src="json2.js& ...

  4. jquery模板制作左侧导航组件

    /** * Created by bmk on 16-4-25. * * 用法:在自己的js里面把左侧导航的相关图标和对应的列表项名称如下编写: * 版本更新日至按需添加 * 在js中的RNA.run ...

  5. mybatis中的mapxml的语法

    <select id="a" resultMap="map"> select * from `table` where (po_type='1') ...

  6. poj3160 强连通+记忆化搜索

    题意:有一张 n 点 m 边的有向无环图,每个点有各自的权值,可正可负,现在从一个点开始走,一直走到不能走到其他点为止,每经过一个点,可以选择获得或不获得它的权值,每个点可以走多次,但是权值只能获得一 ...

  7. (MST) HDOJ 1102 Constructing Roads

    怎么说呢 这题就是个模板题 但是 hud你妹夫啊说好的只有一组数据呢??? 嗯??? wa到家都不认识了好吗 #include <cstdio> #include <cstring& ...

  8. Channel States

    Introduction A channel (a call) will go through many different states during its lifetime. Here we w ...

  9. Python学习笔记——Day2

    一.集成开发环境 集成开发环境(IDE,Integrated development Enviroment)是用于提供程序开发环境的应用程序,一般包括代码编辑器.编译器.调试器和图形用户界面等工具.集 ...

  10. tar

    必要参数有如下: -A 新增压缩文件到已存在的压缩 -B 设置区块大小 -c 建立新的压缩文件 -d 记录文件的差别 -r 添加文件到已经压缩的文件 -u 添加改变了和现有的文件到已经存在的压缩文件 ...