UIImagePickerController拍照与摄像(转)
转载自:http://blog.sina.com.cn/s/blog_68edaff101019ppe.html
(2012-11-23 14:38:40)
//
// 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 = [info objectForKey: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拍照与摄像
该类继承自UINavigationController类 步骤: 检查媒体来源模式是否可用 检查该来源模式下所支持的媒体类型 创建图像选取控制器,设置其属性并显示 在委托协议方法中处理 1.检查媒 ...
- 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 ...
随机推荐
- [设计模式]<<设计模式之禅>>关于迪米特法则
迪米特法则(Law of Demeter,LoD)也称为最少知识原则(Least KnowledgePrinciple,LKP),虽然名字不同,但描述的是同一个规则:一个对象应该对其他对象有最少的了解 ...
- hdu 4685 简单匹配+Tarjan算法
思路:首先看到这题以为能用poj1904的模版直接A掉,WA了几次,然后又TLE了几次.还是想到了正解. 一开始我想的大致方向已经是对的了.先是由王子向每个喜欢的公主建边,再求一次最大匹配,找出匹配后 ...
- Android中定义接口的方法
1.接口方法用于回调(这里定义接口是为了使用其接口方法): public interface ICallback { public void func(); } public class Caller ...
- html accesskey (단축키 지정)
accesskey 속성은 마우스 등을 쓰지 않는 환경을 위해 링크나 입력 폼에서 키보드의 키 입력만으로 동작을 실행할 수 있도록 accesskey 속성값에 access 할 영어 또 ...
- Global.asax中的操作数据库代码无法执行
本人最近在做一个基于Access数据库的Web应用程序,为了实现一个定时更新数据库的需求,我在Global.asax中的Application_Start函数里写了个计时器, void Applica ...
- 每天一道LeetCode--141.Linked List Cycle(链表环问题)
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- asp网站发布步骤总结
1.在VS2012中打开索要发布的网站,初始页可重命名为index.html或default.apx. 2.点击 生成>生成“网站”,然后“发布网站”. 3.进行发布设置: (1 配置文件 ( ...
- 三星智能手机如何运用Smart Switch?
1.Smart Switch是指? 使用前注意事项: 将两部智能手机的距离保持在50cm以内 两部智能手机都下载相同的最新版本Smart Switch 确认可支持的机器参考应用程序说明 不受干扰的安静 ...
- (转)DockPanel的一点点改进
1.当双击Tab时,原先是直接把当前Tab所表示的这个窗体,从主窗体的框架上分离现来,成为一个浮动的窗体.这不是我想要的,我把它改成了双击关闭.在DockPaneStripBase的WndProc方法 ...
- Amoeba相关产品及其介绍
Amoeba for MySQL Amoeba for MySQL致力于MySQL的分布式数据库前端代理层,它主要在应用层访问MySQL的时候充当query 路由功能,专注 分布式数据库 ...