该类继承自UINavigationController类

步骤:
检查媒体来源模式是否可用
检查该来源模式下所支持的媒体类型
创建图像选取控制器,设置其属性并显示
在委托协议方法中处理
 
1.检查媒体来源
调用UIImagePickerController类的静态方法isSourceTypeAvailable来检查
sourceType是一个UIImagePickerControllerSourceType类型的枚举值,它表示图像选取控制器的3种不同的媒体来源模式
UIImagePickerControllerSourceTypePhotoLibrary:照片库模式。图像选取控制器以该模式显示时会浏览系统照片库的根目录。
UIImagePickerControllerSourceTypeCamera:相机模式,图像选取控制器以该模式显示时可以进行拍照或摄像。
UIImagePickerControllerSourceTypeSavedPhotosAlbum:相机胶卷模式,图像选取控制器以该模式显示时会浏览相机胶卷目录。
如果设备支持指定的媒体来源模式,则isSourceTypeAvailable:方法返回YES,否则返回NO。
2.检查支持的媒体类型
调用UIImagePickerController类的另一个静态方法 availableMediaTypesForSourceType:
返回的是字符串数组,kUTTypeImage表示静态图片,kUTTypeMovie表示视频。这两个字符串常量定义在MobileCoreServices框架中。
 
参数info是一个字典,包含媒体类型,拍照的原始图片,编辑后的图片,或是摄像的视频文件的URL等。

//

//  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

 
参考:http://blog.csdn.net/pucker

UIImagePickerController拍照与摄像的更多相关文章

  1. UIImagePickerController拍照与摄像(转)

    转载自:http://blog.sina.com.cn/s/blog_68edaff101019ppe.html (2012-11-23 14:38:40) 标签: ios iphone 拍照 摄像 ...

  2. android之拍照与摄像

    拍照和摄像的意图很简答,这里直接贴代码 布局文件 <?xml version="1.0" encoding="utf-8"?> <Linear ...

  3. 自定义使用AVCaptureSession 拍照,摄像,载图

    转载自 http://blog.csdn.net/andy_jiangbin/article/details/19823333 拍照,摄像,载图总结 1 建立Session  2 添加 input  ...

  4. Android--调用系统照相机拍照与摄像

    前言 在很多场景中,都需要用到摄像头去拍摄照片或视频,在照片或视频的基础之上进行处理.但是Android系统源码是开源的,很多设备厂商均可使用,并且定制比较混乱.一般而言,在需要用到摄像头拍照或摄像的 ...

  5. Android拍照、摄像方向旋转的问题 代码具体解释

    近期做了个拍照.摄像的应用.遇到了拍照.摄像的图像相对于现实.翻转了90度.原因:相机这个硬件的角度是横屏的角度,所以会出现都是横屏的. 1.照相.摄影预览图像的正确角度显 示: public sta ...

  6. AVCaptureSession拍照,摄像,载图总结

    AVCaptureSession [IOS开发]拍照,摄像,载图总结 1 建立Session  2 添加 input  3 添加output  4 开始捕捉 5 为用户显示当前录制状态 6 捕捉 7 ...

  7. swift2.0 UIImagePickerController 拍照 相册 录像

    系统 ios9.1 语言swift2.0 在app 里最常用的功能就是多媒体选择,首先我们storyboard 创建一个button 用于触发选择事件 @IBAction func selectIma ...

  8. UIImagePickerController拍照/相册/录像/本地视频

    1.导入系统库 #import <MobileCoreServices/MobileCoreServices.h> 2.遵守协议 <UIImagePickerControllerDe ...

  9. Android初级教程调用手机拍照与摄像功能

    这个小案例建议在手机上运行. package com.example.camera; import java.io.File; import android.net.Uri; import andro ...

随机推荐

  1. SQLite入门与分析(二)---设计与概念

    写在前面:谢谢各位的关注,没想到会有这么多人关注.高兴的同时,也感到压力,因为我接触SQLite也就几天,也没在实际开发中用过,只是最近项目的需求才来研究它,所以我很担心自己的文章是否会有错误,误导别 ...

  2. SPRING IN ACTION 第4版笔记-第九章Securing web applications-001-SpringSecurity简介(DelegatingFilterProxy、AbstractSecurityWebApplicationInitializer、WebSecurityConfigurerAdapter、@EnableWebSecurity、@EnableWebMvcS)

    一.SpringSecurity的模块 At the least, you’ll want to include the Core and Configuration modules in your ...

  3. C++对象的自销毁

    记得在学校里的时候,曾经这样写过: void MyClass::KillMe() { delete this; } 老师看到这句话的时候,眼珠子都快瞪出来了.但是运行正确啊,没什么问题. 现在想起来, ...

  4. C#基础精华03(常用类库StringBuilder,List<T>泛型集合,Dictionary<K , V> 键值对集合,装箱拆箱)

    常用类库StringBuilder StringBuilder高效的字符串操作 当大量进行字符串操作的时候,比如,很多次的字符串的拼接操作. String 对象是不可变的. 每次使用 System. ...

  5. ENVI Services Engine5.1 应用开发入门教程

    原文地址: ENVI Services Engine5.1 应用开发入门教程_ENVI-IDL中国_新浪博客 http://blog.sina.com.cn/s/blog_764b1e9d0102uy ...

  6. [置顶] Maven多模块项目 eclipse热部署 Maven项目实现 tomcat热部署 二

    最近看到有好多童鞋比较热衷热部署,特别是多模块的项目,其实这热部署如果多模块比较大资源,容易内存溢出或者电脑卡住,并不建议这么做. 不过了解下也没有关系,这里我就在说说热部署的另外一种方法,因为我之前 ...

  7. ARM7ldr指令与ldr伪指令

    ldr伪指令的第二个操作数之前有个=,意思是第一个操作书 = 第二个操作数,相当明了 核心就在于对于用.word指令在.text段里另外定义一段内存,用ldr r0,[pc + x(可以算出.text ...

  8. FastScroll(2)不分组的listview 打开fastscroll的分组提示功能

    本文只让fastscroll具有提示分组功能,但listview并不显示分组,如果想让分组的listview显示fastscroll,看下篇. 1,在listview中打开fastscroll 2,自 ...

  9. 函数buf_ptr_get_fsp_addr

    #define FIL_PAGE_ARCH_LOG_NO_OR_SPACE_ID  34 /****************************************************** ...

  10. 什么是Zookeeper,Zookeeper的作用是什么,在Hadoop及hbase中具体作用是什么

    什么是Zookeeper,Zookeeper的作用是什么,它与NameNode及HMaster如何协作?在没有接触Zookeeper的同学,或许会有这些疑问.这里给大家总结一下. 一.什么是Zooke ...