该类继承自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. C#一个简单下载程序实例(可用于更新)

    运行时的界面 using System; using System.Collections.Generic; using System.ComponentModel; using System.Dat ...

  2. 【BZOJ 2829】 2829: 信用卡凸包 (凸包)

    2829: 信用卡凸包 Description Input Output Sample Input 2 6.0 2.0 0.0 0.0 0.0 0.0 2.0 -2.0 1.5707963268 Sa ...

  3. Nginx的介绍和使用

    http://blog.csdn.net/shimiso/article/details/8690897 1.什么是Nginx Nginx(发音同 engine x)是一款轻量级的Web 服务器/反向 ...

  4. QT UAC问题汇总贴

    http://www.qtcn.org/bbs/read-htm-tid-47983.html http://www.cnblogs.com/bombless/archive/2010/12/29/h ...

  5. 垃圾收集器GC的种类

    堆内存的结构:

  6. TCP/IP的三次握手和四次分手以及超时机制

    使用INADDR_ANY的时候,往往针对多网卡情况,采用tcp连接方式,需要选择使用哪一个网卡发送,自己猜想应该是使用三次握手机制,如何判断目标地址不可达,应该使用的是超时机制,即握手超时则不可到达. ...

  7. Fast scroller styles

    <!-- Fast scroller styles --> <!-- Drawable to use as the fast scroll thumb. --> <att ...

  8. git bash中带空格的文件夹以及文件的处理

    空格用'\ '表示,输入的时候,是不需要单引号的 total 338drwxr-xr-x 9 Administ Administ 4096 Aug 24 23:53 HDTHelperdrwxr-xr ...

  9. 解决angular的post请求后SpringMVC后台接收不到参数值问题的方法

    这是我后台SpringMVC控制器接收isform参数的方法,只是简单的打出它的值: @RequestMapping(method = RequestMethod.POST) @ResponseBod ...

  10. C++学习笔记:不用sizeof判断int类型占用几个字节

    #include <stdio.h> #include <string.h> char *change(int val, int base, char *retbuf) { s ...