iOS 相册相机应用2
在iOS中要拍照和录制视频最简单的方式就是调用UIImagePickerController,UIImagePickerController继承与UINavigationController,需要使用代理方法时需要同时遵守这两个协议,以前可能比较多的是使用UIImagePickerController来选择相册图片或者拍摄图片,其实它的功能还能用来拍摄视频。
使用UIImagePickerController拍照或者拍视频主要以下几个步骤:
创建一个全局的UIImagePickerController对象。
指定UIImagePickerController的来源sourceType,是来自UIImagePickerControllerSourceTypeCamera相机,还是来自UIImagePickerControllerSourceTypePhotoLibrary相册。
然后是设置mediaTypes媒体类型,这是录制视频必须设置的选项,默认情况下是kUTTypeImage(注意:mediaTypes的设置是在MobileCoreServices框架下),同还可以设置一些其他视频相关的属性,例如:videoQuality视频的质量、videoMaximumDuration视频的最大录制时长(默认为10s),cameraDevice摄像头的方向(默认为后置相机)。
指定相机的捕获模式cameraCaptureMode,设置mediaTypes后在设置捕获模式,注意的是捕获模式需要在相机来源sourceType为相机时设置,否则会出现crash。
适时的展示UIImagePickerController,然后在相应的代理方法保存和获取图片或视频。
下面还是上代码吧,更加清晰明了...
首先需要导入以下用到的几个头文件,同时遵守两个代理方法
#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
UIImagePickerController *_imagePickerController;
}
创建UIImagePickerController对象
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
_imagePickerController = [[UIImagePickerController alloc] init];
_imagePickerController.delegate = self;
_imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
_imagePickerController.allowsEditing = YES;
从摄像头获取图片或视频
#pragma mark 从摄像头获取图片或视频
- (void)selectImageFromCamera
{
_imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
//录制视频时长,默认10s
_imagePickerController.videoMaximumDuration = 15;
//相机类型(拍照、录像...)字符串需要做相应的类型转换
_imagePickerController.mediaTypes = @[(NSString *)kUTTypeMovie,(NSString *)kUTTypeImage];
//视频上传质量
//UIImagePickerControllerQualityTypeHigh高清
//UIImagePickerControllerQualityTypeMedium中等质量
//UIImagePickerControllerQualityTypeLow低质量
//UIImagePickerControllerQualityType640x480
_imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
//设置摄像头模式(拍照,录制视频)为录像模式
_imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
[self presentViewController:_imagePickerController animated:YES completion:nil];
}
从相册获取图片或视频
#pragma mark 从相册获取图片或视频
- (void)selectImageFromAlbum
{
//NSLog(@"相册");
_imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:_imagePickerController animated:YES completion:nil];
}
在imagePickerController:didFinishPickingMediaWithInfo:代理方法中处理得到的资源,保存本地并上传...
#pragma mark UIImagePickerControllerDelegate
//该代理方法仅适用于只选取图片时
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo {
NSLog(@"选择完毕----image:%@-----info:%@",image,editingInfo);
}
//适用获取所有媒体资源,只需判断资源类型
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
//判断资源类型
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]){
//如果是图片
self.imageView.image = info[UIImagePickerControllerEditedImage];
//压缩图片
NSData *fileData = UIImageJPEGRepresentation(self.imageView.image, 1.0);
//保存图片至相册
UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
//上传图片
[self uploadImageWithData:fileData];
}else{
//如果是视频
NSURL *url = info[UIImagePickerControllerMediaURL];
//播放视频
_moviePlayer.contentURL = url;
[_moviePlayer play];
//保存视频至相册(异步线程)
NSString *urlStr = [url path];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlStr)) {
UISaveVideoAtPathToSavedPhotosAlbum(urlStr, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
}
});
NSData *videoData = [NSData dataWithContentsOfURL:url];
//视频上传
[self uploadVideoWithData:videoData];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
图片和视频保存完毕后的回调
#pragma mark 图片保存完毕的回调
- (void) image: (UIImage *) image didFinishSavingWithError:(NSError *) error contextInfo: (void *)contextInf{
}
#pragma mark 视频保存完毕的回调
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInf{
if (error) {
NSLog(@"保存视频过程中发生错误,错误信息:%@",error.localizedDescription);
}else{
NSLog(@"视频保存成功.");
}
}
以上仅是简单功能的实现,还有例如切换前后摄像头、闪光灯设置、对焦、曝光模式等更多功能...
链接:http://www.jianshu.com/p/e70a184d1f32
感谢分享
iOS 相册相机应用2的更多相关文章
- iOS相册、相机、通讯录权限获取
iOS相册.相机.通讯录权限获取 说明 这是本人写的一个工具,用以便利的处理各种权限获取的操作,目前提供相册.照相机.通讯录的权限获取操作,参考了 http://www.jianshu.com/p/a ...
- ios中从相册:相机中获取图片信息
ios中从相册/相机中获取图片信息 从相册中获取图片的信息 UIImagePickerController *imgPickView = [[UIImagePickerController alloc ...
- iOS获取相册/相机图片-------自定义获取图片小控件
一.功能简介 1.封装了一个按钮,点击按钮,会提示从何处获取图片:如果设备支持相机,可以从相机获取,同时还可以从手机相册获取图片. 2.选择图片后,有一个block回调,根据需求,将获得的图片拿来使用 ...
- iOS相册中图片按照时间排序
ios相册默认是按照时间从过去到现在排列,图片顺序有正序和逆序,group可以用以下方法来选择顺序 /** @param NSIndexSet 需要获取的相册中图片范围 @param NSEnumer ...
- iOS 相册和网络图片的存取
iOS 相册和网络图片的存取 保存 UIImage 到相册 UIKit UIKit 中一个古老的方法,Objective-C 的形式 void UIImageWriteToSavedPhotosAlb ...
- 阿里聚安全·安全周刊】一种秘密窃取数据的新型 Android 木马|iOS 11相机惊现BUG
本周的七个关键词: 新型 Android 木马丨 TLS 1.3 丨 阿里安全图灵实验室 丨 漏洞感染 Linux 服务器 丨 CPU曝极危漏洞 丨 iOS 11相机BUG 丨R2D2技术 - ...
- iOS 从相机或相册获取图片并裁剪
今天遇到一个用户头像上传的问题,需要从相册或者相机中读取图片.代码很简单,抽取关键部分,如下: //load user image - (void)UesrImageClicked { UIActio ...
- IOS调用相机相册
#import "SendViewController.h" //只能打开,没有加载图片的代码,老代码,供参考 #import <MobileCoreServices/UT ...
- IOS调用相机和相册时无法显示中文
调用系统相册.相机发现是英文的系统相簿界面后标题显示“photos”,但是手机语言已经设置显示中文 需要在info.plist做如下设置 info.plist里面添加 Localizedresourc ...
随机推荐
- border-color: transparent rgb(255, 48, 48) transparent transparent;
border-color: transparent rgb(255, 48, 48) transparent transparent;
- Direct2D教程VII——变换几何(TransformedGeometry)对象
目前博客园中成系列的Direct2D的教程有 1.万一的 Direct2D 系列,用的是Delphi 2009 2.zdd的 Direct2D 系列,用的是VS中的C++ 3.本文所在的 Direct ...
- $#65279导致页面body会出现一个空白行
模板文件生成html文件之后会在body开头处加入一个可见的控制符,导致页面头部会出现一个空白行.原因是页面的编码是UTF-8 + BOM. 直接notopad++ 保存为无BO ...
- 数据驱动测试二:使用TestNG和CSV文件进行数据驱动
转载:https://blog.csdn.net/heart_1014/article/details/52013173 使用@DataProvider注解定义当前方法中的返回对象CSV文件(存放测试 ...
- firefox chrome ie9,10,11 不支持selectSingleNode和selectNodes的解决方法
firefox并不支持selectSingleNode和selectNodes的解决方法 function test(){ var perid = document.thisForm.PerID.va ...
- 入门GTD时间管理系统必读
让我们从什么时间管理开始.什么是时间管理呢?嗯,时间管理就是管理时间.可是,时间怎么能够管理呢? 其实我们管理地并不是时间,而是我们做的事.我们将事情分配到合适的时间段中,在有限的精力中完成它们,得到 ...
- 微软BI 之SSAS 系列 - 关于父子维度的设计
除了之前的几篇文章中出现的时间维度,雪花型维度的设计之外还有一种比较特殊的维度 - 父子维度.父子维度特殊就特殊在它包含了一种基于递归关系(Recursive Relationship)的引用结构, ...
- 通用ajax请求方法封装,兼容主流浏览器
ajax简单介绍 没有AJAX会怎么样?普通的ASP.Net每次运行服务端方法的时候都要刷新当前页面. 假设没有AJAX,在youku看视频的过程中假设点击了"顶.踩".评论.评论 ...
- VIM自定义快捷键 abort
"在选择模式下系统级复制 vmap ,c "+y<ESC>vmap ,C "+Y<ESC>"在选择模式下系统级剪切vmap ,x x:l ...
- 链接sql数据库并输出csv文件
__author__ = 'chunyang.wu' #作者:SelectDB # -*- coding: utf-8 -*- import MySQLdb import os os.environ[ ...