iOS 调用拍照、选择本地相册、上传功能---未完善。
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 调用拍照、选择本地相册、上传功能---未完善。的更多相关文章
- 选择本地文件上传控件 input标签
当要通过控件来选择本地文件上传的时候的一种方式 <input type="file" id="input-file"/> 注意 type类型一定要是 ...
- IOS UIImagePickerController(拍照或者读取相册)
UIImagePickerController ● 使用UIImagePickerController就可以进行拍照或者读取相册 ● 通过sourceType属性来决定拍照还是读取相册 ➢ UII ...
- IOS 调用系统照相机和相册
/** * 调用照相机 */ - (void)openCamera { UIImagePickerController *picker = [[UIImagePickerController all ...
- iOS视频压缩存储至本地并上传至服务器-b
最近做了一个项目,我把其中的核心功能拿出来和大家分享一下,重点还是自己梳理一下. 这里关于视频转码存储我整理了两个方法,这两个方法都是针对相册内视频进行处理的. 1.该方法没有对视频进行压缩,只是将视 ...
- iOS视频压缩存储至本地并上传至服务器
最近做了一个项目,我把其中的核心功能拿出来和大家分享一下,重点还是自己梳理一下. 这里关于视频转码存储我整理了两个方法,这两个方法都是针对相册内视频进行处理的. 1.该方法没有对视频进行压缩,只是将视 ...
- Android WebView选择本地文件上传
This sample demonstrate android webview choose file to upload. I just implement the client code ,the ...
- swift 实现拍照 选择相册
//点击按钮的方法 func photos() { self.showBottomAlert() } /// 屏幕底部弹出的Alert func showBottomAlert(){ let aler ...
- Android 拍照或者从相册获取图片的实现
我们常常会用到上传头像,或者发帖子的时候选择本地图片上传的功能.这个很常见 今天因为app的需求我研究了下.现在分享下. 其实不论是通过拍照还是从相册选取都会用到Intent 这是系统提供给我们用来调 ...
- Android中通过访问本地相册或者相机设置用户头像
目前几乎所有的APP在用户注册时都会有设置头像的需求,大致分为三种情况: (1)通过获取本地相册的图片,经过裁剪后作为头像. (2)通过启动手机相机,现拍图片然后裁剪作为头像. (3)在APP中添加一 ...
随机推荐
- 一些常用的vim设置
以下内容皆来源于网络,感谢原作者.如果引用出处错误,请告知以便修改. 1. vim的几种模式和按键映射 转载自:[1] Map是Vim强大的一个重要原因,可以自定义各种快捷键,用起来自然得心应手.vi ...
- 看啦这么就别人的博客 我也来写一篇! Object转换其他类型
package com.sinitek.framework.util; import java.math.BigDecimal;import java.sql.Timestamp;import jav ...
- 让android项目支持boost 支持c++11
在Application.mk 里增加-D__GLIBC__ 让项目支持boost 增加 -std=c++11 让项目支持c++11 (3.x的cocos本身已经支持了的) 看起来这样: APP_S ...
- codeforces194b
link:http://codeforces.com/problemset/problem/334/B #include <iostream> #include <cstdio> ...
- Codeforces Round #156 (Div. 2)
A. Greg's Workout 模3求和,算最大值. B. Code Parsing 最后左半部分为x,右半部分为y,那么从中间不断去掉xy,直到其中一种全部消去. C. Almost Arith ...
- Latex转换之PDF
近期一直在做如何使用latex将模板转换成PDF.现在写下在项目中如何实现. 1.首先你先进官网下载http://www.miktex.org/download.我用的是如下图所示. 在下载好的Mik ...
- Android——课堂整理:assets目录和手机外部存储
layout文件: <Button android:layout_width="match_parent" android:layout_height="wrap_ ...
- git相关
进入到想要用git管理的project目录下 1.git init 意即该目录会被git监视一切的变动 同时生成一个.git文件夹下面存放了管理该project的一切必要信息 2.git add &l ...
- Updatepanel 注册javascript 方法
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.GetType(), "test", "alert ...
- c++ poco库https调用
#include "Poco\File.h"#include "Poco\FileStream.h"#include "Poco\Process.h& ...