记录于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. Sql查询某个字段是否包含小写字母

    SELECT * from student where username COLLATE Chinese_PRC_CS_AS LIKE '%[abcdefghijklmnopqrstuvwxyz]%'

  2. 一文说尽MySQL事务及ACID特性的实现原理

    MySQL 事务基础概念 事务(Transaction)是访问和更新数据库的程序执行单元:事务中可能包含一个或多个 sql 语句,这些语句要么都执行,要么都不执行.作为一个关系型数据库,MySQL 支 ...

  3. Swift 4 关于Darwin这个Module

    大家在做app或者framework的时候经常会用到生成随机数的方法arc4random系列,或者取绝对值abs()等.所以我有一次好奇的想看看在Developer Document里 是怎么描述这些 ...

  4. 微信小程序开发——点击按钮退出小程序的实现

    微信小程序官方是没有提供退出的API的,但是在navigator这个组件中,是有退出这个功能的:详情参考官方文档:navigator.示例代码:1 navigator open-type=" ...

  5. 2018-2019-2 20175306实验一《Java开发环境的熟悉》实验报告

    2018-2019-2 20175306实验一<Java开发环境的熟悉>实验报告 一.实验内容及步骤 实验一 Java开发环境的熟悉-1 ·建立有自己学号的实验目录. ·通过vim Hel ...

  6. 华南理工大学“三七互娱杯”程序设计竞赛(重现赛)( HRY and array 高精度除法模板)

    题目链接:https://ac.nowcoder.com/acm/contest/874/D 题目大意:给你两个数列a和b然后对a可以进行排列,对b可以任意排列,问你sigma(a(i)*b(i))的 ...

  7. python模块------shutil

    说明 shutil -- High-level file operations 是一种高层次的文件操作工具 类似于高级API,而且主要强大之处在于其对文件的复制与删除操作更是比较支持好. copy() ...

  8. centos7.6设置sftp服务

    sftp是Secure File Transfer Protocol的缩写,安全文件传送协议. CentOS自带 SSH 服务,直接配置即可 1. 查看ssh版本 sftp是基于ssh协议的,首先查看 ...

  9. iOS程序依赖管理的工具——CocoaPods

    1. 简介 CocoaPods是一个负责管理iOS项目中第三方开源代码的工具,其源码在Github上开源.使用CocoaPods可以节省设置和更新第三方开源库的时间并提高工作效率. 2. CocoaP ...

  10. git rejected - non-fast-forward

    di第一次提交时可能提示此错误,应该是.gitignore冲突,建议码云创建时不要初始化,如果已经出现了.可以从git  repostitory里合并. 参考:https://blog.csdn.ne ...