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 ...
随机推荐
- 查看使用的Eclipse版本
第一种方法 1. 找到Eclipse的解压目录就是你的Eclipse.exe 所在的目录 2. 找到 .eclipseproduct 文件双击打开
- Eclipse执行Hadoop WordCount
前期工作 我的Eclipse是安装在Windows下的,通过Eclipse执行程序连接Hadoop, 需要让虚拟机的访问地址和本机的访问地址保持在同一域内,虚拟机的地址更改前面的文章介绍过了,如果想改 ...
- LoadImage 和 BitBlt
#include <windows.h> #define WINDOWCLASS TEXT("Test") #define WNDTITLE TEXT("Te ...
- C语言字节对齐
转自:http://blog.csdn.net/21aspnet/article/details/6729724 文章最后本人做了一幅图,一看就明白了,这个问题网上讲的不少,但是都没有把问题说透. 一 ...
- 在C++中调用DLL中的函数
如何在C++中调用DLL中的函数 应用程序使用DLL可以采用两种方式:一种是隐式链接,另一种是显式链接.在使用DLL之前首先要知道DLL中函数的结构信息.Visual C++6.0在VC\bin目录下 ...
- ogg实现oracle到sql server 2005的同步
一.源端(oracle)配置1.创建同步测试表create table gg_user.t01(name varchar(20) primary key);create table gg_user.t ...
- .NET中各种不同的Timer之间区别
System.Timer.Timer 根据命名空间看这个类貌似才是标准的Timer,它提供Interval属性和Elapsed事件.可以每隔一个时间周期触发一次Elapsed事件.在ThreadPoo ...
- JAX-RS入门 二 :运行
上一节,已经成功的定义了一个REST服务,并且提供了具体的实现,不过我们还需要把它运行起来. 在上一节的装备部分,列举了必须的jar(在tomcat中运行)和可选的jar(作为一个独立的应用程序运行) ...
- js如何判断是手机端还是PC端访问
function isMobile(){ var sUserAgent= navigator.userAgent.toLowerCase(), bIsIpad= sUserAgent.match(/i ...
- 解决iframe缓存
网上能搜到很多此类的资料,但都是互相转载,不太起作用.这几天写个用到,用了不少的iframe效果.结果发现iframe有缓存的问题.网上提供了思路,即对iframe的href后添加随即get值,来逃避 ...