记录于2013/7/4
 
加入框架: 
MobileCoreServices.framework 
MediaPlayer.framework
 
导入头文件:
#import <MediaPlayer/MediaPlayer.h>
#import <MobileCoreServices/UTCoreTypes.h> 
 

代码: 
if(![UIImagePickerControllerisSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera]) {
takePictureButton.hidden = YES; //无摄像头隐藏改按钮
}
imageFrame = imageView.frame;

实现:

- (IBAction)shootPictureOrVideo:(id)sender {
[selfgetMediaFromSource:UIImagePickerControllerSourceTypeCamera];
}
- (IBAction)selectExistingPictureOrVideo:(id)sender {
[selfgetMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];
}
#pragma mark UIImagePickerController delegate methods
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
self.lastChosenMediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([lastChosenMediaTypeisEqual:(NSString *)kUTTypeImage]) {
UIImage *chosenImage = [info objectForKey:UIImagePickerControllerEditedImage];
UIImage *shrunkenImage = shrinkImage(chosenImage, imageFrame.size);
self.image = shrunkenImage;
} elseif ([lastChosenMediaTypeisEqual:(NSString *)kUTTypeMovie]) {
self.movieURL = [info objectForKey:UIImagePickerControllerMediaURL];
}
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissModalViewControllerAnimated:YES];
}
#pragma mark -
//类扩张
staticUIImage *shrinkImage(UIImage *original, CGSize size) {
//设备物理屏幕像素倍数
CGFloat scale = [UIScreenmainScreen].scale;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(NULL, size.width * scale,
size.height * scale, , , colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context,
CGRectMake(, , size.width * scale, size.height * scale),
original.CGImage);
CGImageRef shrunken = CGBitmapContextCreateImage(context);
UIImage *final = [UIImageimageWithCGImage:shrunken]; CGContextRelease(context);
CGImageRelease(shrunken); return final;
}
- (void)updateDisplay {
if ([lastChosenMediaTypeisEqual:(NSString *)kUTTypeImage]) {
imageView.image = image;
imageView.hidden = NO;
moviePlayerController.view.hidden = YES;
} elseif ([lastChosenMediaType isEqual:(NSString *)kUTTypeMovie]) {
[self.moviePlayerController.view removeFromSuperview];
self.moviePlayerController = [[MPMoviePlayerControlleralloc]
initWithContentURL:movieURL];
moviePlayerController.view.frame = imageFrame;
moviePlayerController.view.clipsToBounds = YES;
[self.viewaddSubview:moviePlayerController.view];
imageView.hidden = YES;
}
}
- (void)getMediaFromSource:(UIImagePickerControllerSourceType)sourceType {
//判断设备是否具有该功能
NSArray *mediaTypes = [UIImagePickerController
availableMediaTypesForSourceType:sourceType];
if ([UIImagePickerControllerisSourceTypeAvailable:
sourceType] && [mediaTypes count] > ) {
NSArray *mediaTypes = [UIImagePickerController
availableMediaTypesForSourceType:sourceType];
UIImagePickerController *picker =
[[UIImagePickerController alloc] init];
picker.mediaTypes = mediaTypes;
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = sourceType;
[self presentModalViewController:picker animated:YES];
} else {
//设备不支持该功能,弹出提示
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error accessing media"
message:@"Device doesn’t support that media source."
delegate:nil
cancelButtonTitle:@"Drat!"
otherButtonTitles:nil];
[alert show];
}
}

UIImagePickerController照片选取器的更多相关文章

  1. iOS:图像选取器控制器控件UIImagePickerController的详解

    图像选择控制器:UIImagePickerController 功能:用于选取相册或相机等里面的照片. @interface UIImagePickerController : UINavigatio ...

  2. UWP入门(十一)--使用选取器打开文件和文件夹

    原文:UWP入门(十一)--使用选取器打开文件和文件夹 很漂亮的功能,很有趣 重要的 API FileOpenPicker FolderPicker StorageFile 通过让用户与选取器交互来访 ...

  3. Win10系统怎样让打开图片方式为照片查看器

    转载自:百度经验 http://jingyan.baidu.com/article/5d368d1ef0cad13f60c057e3.html 1.首先,我们需要使用注册表编辑器来开启Win10系统照 ...

  4. 在Windows 10下启用旧的照片查看器

    从Windows 10开始,默认只能通过“照片”来查看图片了,非常不方便!通过将下列文本保存在.reg文件后导入,即可找回Windows XP时代的“照片查看器”. Windows Registry ...

  5. 解决Win10图片打开方式没有“Windows照片查看器”问题

    1.打开注册表编辑器(Win+R,Regedit),定位至(建议修改前备份注册表): HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Photo Viewe ...

  6. UIDatePicker日期选取器

    //定义显示日期的格式 NSDateFormatter *dateFormat = [[NSDateFormatter alloc]init]; //NSDateFormatterMediumStyl ...

  7. Windows Store App JavaScript 开发:文件选取器

    正如前面章节C#语言中所介绍的,文件选取器是应用与系统进行交互的一个接口,通过文件选取器可以在应用中直接与文件系统进行交互,访问不同位置的文件或文件夹,或者将文件存储在指定位置.文件选取器分为对文件进 ...

  8. Window Server 2012 R2 没有照片查看器 打开图片都是画板问题怎么解决

    新安装了 Window Server 2012 R2 系统,感觉屌屌的样子,加上开机速度蛮快,心里略爽.结果,打开图片一看,发现竟然是画板,而且还没有照片查看器,顿时泪流满面. 后来我利用了强大的百度 ...

  9. 【WP 8.1开发】文件选取器的使用方法

    在以往的WP7x/8.0开发中,我们使用选择器可以浏览并打开图片.音频.视频等一些特殊文件,在8.0 SDK中的运行时API(从Win 8 app中移植)尽管提供了Windows.Storage.Pi ...

随机推荐

  1. Java调用第三方http接口的方式

    1. 概述 在实际开发过程中,我们经常需要调用对方提供的接口或测试自己写的接口是否合适.很多项目都会封装规定好本身项目的接口规范,所以大多数需要去调用对方提供的接口或第三方接口(短信.天气等). 在J ...

  2. mysql 一张表的数据插入另一张表的sql语句

    1. 表结构完全一样 insert into 表1 select * from 表2 2. 表结构不一样(这种情况下得指定列名) insert into 表(列名1,列名2,列名3) select 列 ...

  3. (Python3 自定义函数实现数字金字塔 代码

    def kzkz(ceng): for i in range(1,ceng+1): print(" "*(ceng-i),end='') n=i while(n>=1): p ...

  4. 模块--random

    random模块 1       random.random() print(random.random()) 0-1 之间随机小数 不包含1 2       random.uniform(a,b) ...

  5. 初识springboot(傻瓜式教程)

    初识springboot(傻瓜式教程) 项目所需的版本 IDEA 2018 maven 3.x jdk-1.8 IDEA创建spring-boot项目(maven方法) 1.创建一个maven工程 点 ...

  6. 有关mysql索引

    1.首先我们需要明确一下什么是索引以及为什么要使用索引: 索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结构.在生产环境中,对于数据库我们最常进行的是查询的操作,而当我们的数据非 ...

  7. Theano.tensor.round函数学习,同时解决输出Elemwise{xxx,no_inplace}.0的问题

    1. 出现Elemwise{xxx,no_inplace}.0   这是因为没有定义theano.function所致,参考下面错误示范: y = np.random.normal(size=(2,2 ...

  8. # 20175333曹雅坤《Java程序设计》第2周学习总结

    教材学习内容总结 1.学习第二,三章ppt,并观看视频. 2.在虚拟机中连接到码云,克隆代码,编译与运行教材上的例子. 3.在虚拟机上安装相关配置,使其满足学习要求. 4.运行并截图上传监督学习脚本s ...

  9. 【转】监听器(Listener)学习

    一.监听器介绍 1.1.监听器的概念 监听器是一个专门用于对其它对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监听的对象发生情况时,立即采取相应的行动.监听器其实就是一个实现特定接口的普通 ...

  10. maven父子模块deploy 问题

    1.问题描述:直接 deploy 子某块,但第三者确不能引用 jar(jar down不下来,但pom不报错) 原因:父子模块项目必须保证父模块pom deploy后,再 deploy 子模块才能被引 ...