UIImagePickerController拍照与摄像
该类继承自UINavigationController类
//
// ViewController.h
// Camera
//
// Created by gao wuhang on 12-11-23.
// Copyright (c) 2012年 gao wuhang. All rights reserved.
//
#import
@interface ViewController : UIViewController<</span>UINavigationControllerDelegate,UIImagePickerControllerDelegate>
- (IBAction)takePictureButtonClick:(id)sender;
- (IBAction)captureVideoButtonClick:(id)sender;
@end
//
// ViewController.m
// Camera
//
// Created by gao wuhang on 12-11-23.
// Copyright (c) 2012年 gao wuhang. All rights reserved.
//
#import "ViewController.h"
#import
#import
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)takePictureButtonClick:(id)sender{
//检查相机模式是否可用
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSLog(@"sorry, no camera or camera is unavailable.");
return;
}
//获得相机模式下支持的媒体类型
NSArray* availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
BOOL canTakePicture = NO;
for (NSString* mediaType in availableMediaTypes) {
if ([mediaType isEqualToString:(NSString*)kUTTypeImage]) {
//支持拍照
canTakePicture = YES;
break;
}
}
//检查是否支持拍照
if (!canTakePicture) {
NSLog(@"sorry, taking picture is not supported.");
return;
}
//创建图像选取控制器
UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
//设置图像选取控制器的来源模式为相机模式
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
//设置图像选取控制器的类型为静态图像
imagePickerController.mediaTypes = [[[NSArray alloc] initWithObjects:(NSString*)kUTTypeImage, nil] autorelease];
//允许用户进行编辑
imagePickerController.allowsEditing = YES;
//设置委托对象
imagePickerController.delegate = self;
//以模视图控制器的形式显示
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];
}
- (IBAction)captureVideoButtonClick:(id)sender{
//检查相机模式是否可用
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
NSLog(@"sorry, no camera or camera is unavailable!!!");
return;
}
//获得相机模式下支持的媒体类型
NSArray* availableMediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
BOOL canTakeVideo = NO;
for (NSString* mediaType in availableMediaTypes) {
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
//支持摄像
canTakeVideo = YES;
break;
}
}
//检查是否支持摄像
if (!canTakeVideo) {
NSLog(@"sorry, capturing video is not supported.!!!");
return;
}
//创建图像选取控制器
UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
//设置图像选取控制器的来源模式为相机模式
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
//设置图像选取控制器的类型为动态图像
imagePickerController.mediaTypes = [[[NSArray alloc] initWithObjects:(NSString*)kUTTypeMovie, nil] autorelease];
//设置摄像图像品质
imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
//设置最长摄像时间
imagePickerController.videoMaximumDuration = 30;
//允许用户进行编辑
imagePickerController.allowsEditing = YES;
//设置委托对象
imagePickerController.delegate = self;
//以模式视图控制器的形式显示
[self presentModalViewController:imagePickerController animated:YES];
[imagePickerController release];
}
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo{
if (!error) {
NSLog(@"picture saved with no error.");
}
else
{
NSLog(@"error occured while saving the picture%@", error);
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
//打印出字典中的内容
NSLog(@"get the media info: %@", info);
//获取媒体类型
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
//判断是静态图像还是视频
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
//获取用户编辑之后的图像
UIImage* editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
//将该图像保存到媒体库中
UIImageWriteToSavedPhotosAlbum(editedImage, self,@selector(image:didFinishSavingWithError:contextInfo:), NULL);
}else if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
//获取视频文件的url
NSURL* mediaURL = [infoobjectForKey:UIImagePickerControllerMediaURL];
//创建ALAssetsLibrary对象并将视频保存到媒体库
ALAssetsLibrary* assetsLibrary = [[ALAssetsLibrary alloc]init];
[assetsLibrary writeVideoAtPathToSavedPhotosAlbum:mediaURLcompletionBlock:^(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];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
UIImagePickerController拍照与摄像的更多相关文章
- UIImagePickerController拍照与摄像(转)
转载自:http://blog.sina.com.cn/s/blog_68edaff101019ppe.html (2012-11-23 14:38:40) 标签: ios iphone 拍照 摄像 ...
- android之拍照与摄像
拍照和摄像的意图很简答,这里直接贴代码 布局文件 <?xml version="1.0" encoding="utf-8"?> <Linear ...
- 自定义使用AVCaptureSession 拍照,摄像,载图
转载自 http://blog.csdn.net/andy_jiangbin/article/details/19823333 拍照,摄像,载图总结 1 建立Session 2 添加 input ...
- Android--调用系统照相机拍照与摄像
前言 在很多场景中,都需要用到摄像头去拍摄照片或视频,在照片或视频的基础之上进行处理.但是Android系统源码是开源的,很多设备厂商均可使用,并且定制比较混乱.一般而言,在需要用到摄像头拍照或摄像的 ...
- Android拍照、摄像方向旋转的问题 代码具体解释
近期做了个拍照.摄像的应用.遇到了拍照.摄像的图像相对于现实.翻转了90度.原因:相机这个硬件的角度是横屏的角度,所以会出现都是横屏的. 1.照相.摄影预览图像的正确角度显 示: public sta ...
- AVCaptureSession拍照,摄像,载图总结
AVCaptureSession [IOS开发]拍照,摄像,载图总结 1 建立Session 2 添加 input 3 添加output 4 开始捕捉 5 为用户显示当前录制状态 6 捕捉 7 ...
- swift2.0 UIImagePickerController 拍照 相册 录像
系统 ios9.1 语言swift2.0 在app 里最常用的功能就是多媒体选择,首先我们storyboard 创建一个button 用于触发选择事件 @IBAction func selectIma ...
- UIImagePickerController拍照/相册/录像/本地视频
1.导入系统库 #import <MobileCoreServices/MobileCoreServices.h> 2.遵守协议 <UIImagePickerControllerDe ...
- Android初级教程调用手机拍照与摄像功能
这个小案例建议在手机上运行. package com.example.camera; import java.io.File; import android.net.Uri; import andro ...
随机推荐
- linux grep命令总结
风生水起善战者,求之于势,不责于人,故能择人而任势. 博客园 首页 新随笔 联系 订阅 管理 posts - 791, comments - 394, trackba ...
- [itint5]判断是否为二叉搜索树
http://www.itint5.com/oj/#25 这题在leetcode上是用中序遍历来做的,但是这里由于有相等的情况,即左子树小于等于根,这样中序遍历无法完全判定.可以用递归来做,用递归给出 ...
- ANDROID_MARS学习笔记_S01_004dpi、dp(dip)及计算
一.dpi.dp介绍 sp会随着用户在手机中设置字体大小而改变,而dp不会 二.1.dpsp_layout.xml <?xml version="1.0" encoding= ...
- libevent学习之二:Windows7(Win7)下编译libevent
Linux下编译参考源码中的README文件即可,这里主要记录Windows下的编译. 一.准备工作 去官网下载最新的稳定发布版本libevent-2.0.22-stable 官网地址:http:// ...
- JavaScript 获取客户端计算机硬件及系统信息
1.浏览器信息 //浏览器信息 function BrowserInfo() { var userLanguage = navigator.userLanguage; // 用户在自己的操作系 ...
- Random Integer Generator
先占坑.以后再修改 昨天遇到一道题, Given int Rand(1) = 0或者 1- uniformly distributed, write a function to implemen ...
- SDIBT2666——逆波兰表达式求值
逆波兰表达式求值(栈和队列) Description 从键盘上输入一个逆波兰表达式,用伪码写出其求值程序.规定:逆波兰表达式的长度不超过一行,以@符作为输入结束,操作数之间用空格分隔,操作符只可能有+ ...
- Android安全问题 钓鱼程序
导读:文本介绍一种钓鱼应用,讲述如何骗取用户的用户名和密码,无须root 这个话题是继续android安全问题(二) 程序锁延伸的 之前我已经展示了如何制作程序锁.当打开指定应用的时候,弹出一个密码页 ...
- Django处理文件上传File Uploads
HttpRequest.FILES 表单上传的文件对象存储在类字典对象request.FILES中,表单格式需为multipart/form-data <form enctype="m ...
- PHP微信公众平台开发1 配置接口
1.简介 微信公众平台是腾讯公司在微信的基础上新增的功能模块,通过这一平台,个人和企业都可以打造一个微信的公众号,并实现和特定群体的文字.图片.语音的全方位沟通.互动. 2.通讯机制 3.注册微信公众 ...