iOS摄像头和相册(转)
iOS摄像头和相册iOS 获取图片有三种方法1. 直接调用摄像头拍照
2. 从相册中选择
3. 从图库中选择
UIImagePickerController 是系统提供的用来获取图片和视频的接口;
用UIImagePickerController 类来获取图片视频,大体分为以下几个步骤:
1. 初始化UIImagePickerController 类;
2. 设置UIImagePickerController 实例的数据来源类型
3 设置代理
4. 如果需要做图片修改的话设置allowsEditing =yes。
-UIImagePickerController-浅析
(2013-04-05 19:16:26)
标签:
uiimagepickercontrolios摄像头ios相册iphone |
分类: 帮助文档 |
UIImagePickerController 这个类可以为大家提供照相的功能,以及图片,视频浏览的功能。
二、
#pragma mark - 摄像头和相册相关的公共类
// 判断设备是否有摄像头
- (BOOL) isCameraAvailable{
return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera];
}
// 前面的摄像头是否可用
- (BOOL) isFrontCameraAvailable{
return [UIImagePickerControllerisCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront];
}
// 后面的摄像头是否可用
- (BOOL) isRearCameraAvailable{
return [UIImagePickerControllerisCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];
}
// 判断是否支持某种多媒体类型:拍照,视频
- (BOOL) cameraSupportsMedia:(NSString *)paramMediaType sourceType:(UIImagePickerControllerSourceType)paramSourceType{
__block BOOL result = NO;
if ([paramMediaType length] == 0){
NSLog(@"Media type is empty.");
return NO;
}
NSArray *availableMediaTypes =[UIImagePickerControlleravailableMediaTypesForSourceType:paramSourceType];
[availableMediaTypes enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL*stop) {
NSString *mediaType = (NSString *)obj;
if ([mediaTypeisEqualToString:paramMediaType]){
result = YES;
*stop= YES;
}
}];
return result;
}
// 检查摄像头是否支持录像
- (BOOL) doesCameraSupportShootingVideos{
return [self cameraSupportsMedia:( NSString *)kUTTypeMoviesourceType:UIImagePickerControllerSourceTypeCamera];
}
// 检查摄像头是否支持拍照
- (BOOL) doesCameraSupportTakingPhotos{
return [self cameraSupportsMedia:( NSString *)kUTTypeImagesourceType:UIImagePickerControllerSourceTypeCamera];
}
#pragma mark - 相册文件选取相关
// 相册是否可用
- (BOOL) isPhotoLibraryAvailable{
return [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary];
}
// 是否可以在相册中选择视频
- (BOOL) canUserPickVideosFromPhotoLibrary{
return [self cameraSupportsMedia:( NSString *)kUTTypeMoviesourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
// 是否可以在相册中选择视频
- (BOOL) canUserPickPhotosFromPhotoLibrary{
return [self cameraSupportsMedia:( NSString *)kUTTypeImagesourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
#pragma mark - 拍照按钮事件
- (void)ClickControlAction:(id)sender{
// 判断有摄像头,并且支持拍照功能
if ([self isCameraAvailable] && [self doesCameraSupportTakingPhotos]){
// 初始化图片选择控制器
UIImagePickerController *controller = [[UIImagePickerController alloc] init];
[controller setSourceType:UIImagePickerControllerSourceTypeCamera];// 设置类型
// 设置所支持的类型,设置只能拍照,或则只能录像,或者两者都可以
NSString *requiredMediaType = ( NSString *)kUTTypeImage;
NSString *requiredMediaType1 = ( NSString *)kUTTypeMovie;
NSArray *arrMediaTypes=[NSArray arrayWithObjects:requiredMediaType, requiredMediaType1,nil];
[controller setMediaTypes:arrMediaTypes];
// 设置录制视频的质量
[controller setVideoQuality:UIImagePickerControllerQualityTypeHigh];
//设置最长摄像时间
[controller setVideoMaximumDuration:10.f];
[controller setAllowsEditing:YES];// 设置是否可以管理已经存在的图片或者视频
[controller setDelegate:self];// 设置代理
[self.navigationController presentModalViewController:controller animated:YES];
[controller release];
} else {
NSLog(@"Camera is not available.");
}
}
typedef NS_ENUM(NSInteger, UIImagePickerControllerSourceType) {
UIImagePickerControllerSourceTypePhotoLibrary,
UIImagePickerControllerSourceTypeCamera,
UIImagePickerControllerSourceTypeSavedPhotosAlbum
};
// 设置所支持的类型,设置只能拍照,或则只能录像,或者两者都可以
NSString *requiredMediaType = ( NSString *)kUTTypeImage;
NSString *requiredMediaType1 = ( NSString *)kUTTypeMovie;
NSArray *arrMediaTypes=[NSArray arrayWithObjects:requiredMediaType, requiredMediaType1,nil];
[controller setMediaTypes:arrMediaTypes];
#pragma mark - UIImagePickerControllerDelegate 代理方法
// 保存图片后到相册后,调用的相关方法,查看是否保存成功
- (void) imageWasSavedSuccessfully:(UIImage *)paramImage didFinishSavingWithError:(NSError *)paramError contextInfo:(void *)paramContextInfo{
if (paramError == nil){
NSLog(@"Image was saved successfully.");
} else {
NSLog(@"An error happened while saving the image.");
NSLog(@"Error = %@", paramError);
}
}
// 当得到照片或者视频后,调用该方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSLog(@"Picker returned successfully.");
NSLog(@"%@", info);
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
// 判断获取类型:图片
if ([mediaType isEqualToString:( NSString *)kUTTypeImage]){
UIImage *theImage = nil;
// 判断,图片是否允许修改
if ([picker allowsEditing]){
//获取用户编辑之后的图像
theImage = [info objectForKey:UIImagePickerControllerEditedImage];
} else {
// 照片的元数据参数
theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
}
// 保存图片到相册中
SEL selectorToCall = @selector(imageWasSavedSuccessfully:didFinishSavingWithError:contextInfo:);
UIImageWriteToSavedPhotosAlbum(theImage, self,selectorToCall, NULL);
}else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie]){
// 判断获取类型:视频
//获取视频文件的url
NSURL* mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
//创建ALAssetsLibrary对象并将视频保存到媒体库
// Assets Library 框架包是提供了在应用程序中操作图片和视频的相关功能。相当于一个桥梁,链接了应用程序和多媒体文件。
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibrary alloc] init];
// 将视频保存到相册中
[assetsLibrary writeVideoAtPathToSavedPhotosAlbum:mediaURL
completionBlock:^(NSURL *assetURL,NSError *error) {
if (!error) {
NSLog(@"captured video saved with no error.");
}else{
NSLog(@"error occured while saving the video:%@", error);
}
}];
[assetsLibrary release];
}
[picker dismissModalViewControllerAnimated:YES];
}
// 当用户取消时,调用该方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissModalViewControllerAnimated:YES];
}
#pragma mark - 相册操作
- (void)ClickShowPhotoAction:(id)sender{
if ([self isPhotoLibraryAvailable]){
UIImagePickerController *controller = [[UIImagePickerController alloc]init];
[controller setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];// 设置类型
NSMutableArray *mediaTypes = [[NSMutableArray alloc] init];
if ([self canUserPickPhotosFromPhotoLibrary]){
[mediaTypes addObject:( NSString *)kUTTypeImage];
}
if ([self canUserPickVideosFromPhotoLibrary]){
[mediaTypes addObject:( NSString *)kUTTypeMovie];
}
[controller setMediaTypes:mediaTypes];
[controller setDelegate:self];// 设置代理
[self.navigationController presentModalViewController:controlleranimated:YES];
[controller release];
[mediaTypes release];
}
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info方法,中的info值,会根据我们操作的类型不同,而产生了不同的数据信息:
当操作的为图片时::
{
UIImagePickerControllerCropRect = "NSRect: {{0, 405}, {2448, 2449}}";
UIImagePickerControllerEditedImage = "";
UIImagePickerControllerMediaMetadata = {
DPIHeight = 72;
DPIWidth = 72;
Orientation = 6;
"{Exif}" = {
ApertureValue = "2.526068811667588";
BrightnessValue = "-0.0709875088566263";
ColorSpace = 1;
DateTimeDigitized = "2013:04:05 16:43:00";
DateTimeOriginal = "2013:04:05 16:43:00";
ExposureMode = 0;
ExposureProgram = 2;
ExposureTime = "0.05882352941176471";
FNumber = "2.4";
Flash = 24;
FocalLenIn35mmFilm = 35;
FocalLength = "4.28";
ISOSpeedRatings = (
400
);
MeteringMode = 5;
PixelXDimension = 3264;
PixelYDimension = 2448;
SceneType = 1;
SensingMethod = 2;
Sharpness = 0;
ShutterSpeedValue = "4.099543917546131";
SubjectArea = (
1631,
1223,
881,
881
);
WhiteBalance = 0;
};
"{TIFF}" = {
DateTime = "2013:04:05 16:43:00";
Make = Apple;
Model = "iPhone 4S";
Software = "5.1.1";
XResolution = 72;
YResolution = 72;
};
};
UIImagePickerControllerMediaType = "public.image";
UIImagePickerControllerOriginalImage = "";
}
{
UIImagePickerControllerMediaType = "public.movie";
UIImagePickerControllerMediaURL = "file://localhost/private/var/mobile/Applications/22A14825-DD7E-48E1-A1D5-2D85B82095B5/tmp/capture-T0x1363a0.tmp.etXfD4/capturedvideo.MOV";
}
iOS摄像头和相册(转)的更多相关文章
- iOS摄像头和相册-UIImagePickerController常用操作
在一些应用中,我们需要用到iOS设备的摄像头进行拍照,视频.并且从相册中选取我们需要的图片或者视频. 关于iOS摄像头和相册的应用,可以使用UIImagePickerController类来完成控制. ...
- iOS摄像头和相册-UIImagePickerController-浅析
转载自:http://blog.sina.com.cn/s/blog_7b9d64af0101cfd9.html 在一些应用中,我们需要用到iOS设备的摄像头进行拍照,视频.并且从相册中选取我们需要的 ...
- iOS摄像头和相册-UIImagePickerController-浅析(转)
iOS摄像头和相册-UIImagePickerController-浅析(转) 转自: http://blog.sina.com.cn/s/blog_7b9d64af0101cfd9.html 在一些 ...
- ios获取摄像头与相册图片
iOS的一些设备上都安装了摄像头.现在绝大多数都有了. 在编程中,我们是用相应的东西来进行照相,录像等功能. 一.UIImagePickerController类 UIImagePickerCon ...
- ios中从相册:相机中获取图片信息
ios中从相册/相机中获取图片信息 从相册中获取图片的信息 UIImagePickerController *imgPickView = [[UIImagePickerController alloc ...
- html5调取手机摄像头或相册
html5调用手机摄像头或者相册 由于input的type=file 格式的文件的界面并不是我们所希望的界面,所以在此我隐藏input,自定义样式,这个样式就在<a>中自己定义,这里我就不 ...
- iOS频繁打开相册崩溃: ALAssetsLibrary error - “Too many contexts. No space in contextList.”
iOS频繁打开相册崩溃: ALAssetsLibrary error - “Too many contexts. No space in contextList.” http://stackoverf ...
- IOS 保存图片至相册
IOS 保存图片至相册 应用中有时我们会有保存图片的需求,如利用UIImagePickerController用IOS设备内置的相机拍照,或是有时我们在应用程序中利用UIKit的 UIGraphi ...
- android基础---->摄像头与相册的调用
很多应用程序都可能会使用到调用摄像头拍照和从相册选取图片的功能,今天我们开始android中摄像头与相册调用的学习. 目录导航 调用摄像头拍照 从相册中选择照片 友情链接 调用摄像头拍照 大致流程:调 ...
随机推荐
- logging 为全局的日志工具对象添加日志记录器
def log_file(LEVEL_NAME): # 设置日志的记录等级,常见的有四种,大小关系如下,DEBUG < INFO < WARNING <ERROR # 一旦设置级别, ...
- 28-SQLServer带见证服务器的镜像搭建
一.注意点 1.数据库的模式要是完整模式. 2.要对数据库完整备份和事务日志备份,分别还原到镜像库上,使用NORECOVERY模式. 3.镜像数据库是不允许删除和操作,即便查看属性也不行. 4.先删除 ...
- this 总结
谁最终调用函数,this指向谁!!! ① this指向的,永远只可能是对象! ② this指向谁,永远不取决于this写在哪!而是取决于函数在哪调用. ③ this指向的对象,我们称之为函数的 ...
- firewall 相关命令
1.firewalld的基本使用 启动: systemctl start firewalld 查看状态: systemctl status firewalld 停止: systemctl disabl ...
- 第12章、乐活人生的ABCDE
目录 第12章.乐活人生的ABCDE 什么时候该乐观 让自己乐观的ABC 确认ABC 你的ABC记录 反驳和转移注意 转移注意 反驳 保持距离 学习与自己争辩 证据 其他可能性 暗示 用处 你的反驳记 ...
- spark map函数中使用println无法输出
问题 // 每个点为hardData中的一个Array val hardData = spark.read.textFile(args(0)).rdd .map(_.split(" &quo ...
- 用Python实现自己下载音乐的统计
今天看Python实例,学习了如何对文件进行操作,突然想把自己网易云音乐下载到本地的歌曲名单写到一个txt中,看看具体情况.当然,我现在肯定无法做到直接去网易云音乐上爬取,就做个最简单的吧,嘻嘻^-^ ...
- 正则re.complie作用
封装一个原本重复使用的正则表达式 prog = re.compile(pattern) result = prog.match(string)
- 结构体与C++sort()函数的用法
C++sort()函数的用法 先看道题(本题来自http://codeup.cn/第26099): 题目描述 [问题描述] 高三全省联合模拟考试刚刚结束,经过各科老师的努力,各个学科的改卷工作终于结束 ...
- ROS常用命令
ROS常用命令 打印ros环境变量 $ echo $ROS_PACKAGE_PATH 确认环境变量已经设置正确 export | grep ROS 环境变量设置文件 sudo gedit ./.bas ...