多媒体之调用相机相册及视频播放

 1 #import "RootViewController.h"
 2 #import <MediaPlayer/MediaPlayer.h>
 3 #import <MobileCoreServices/MobileCoreServices.h> //存放了系统调用的常量
 4 @interface RootViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
 5 
 6 @end
 7 
 8 @implementation RootViewController
 9 
10 - (void)viewDidLoad {
11     [super viewDidLoad];
12     // Do any additional setup after loading the view.
13     [self createUI];
14 }
15 -(void)createUI{
16     NSMutableArray *titleAry=[[NSMutableArray alloc] initWithObjects:@"拍照",@"相册",@"视频", nil];
17     for (int i=0; i<titleAry.count; i++) {
18         UIButton *btn=[[UIButton alloc] initWithFrame:CGRectMake(100, 100+i*100, 200, 50)];
19         btn.backgroundColor=[UIColor blackColor];
20         [btn setTitle:titleAry[i] forState:UIControlStateNormal];
21         btn.tag=1+i;
22         [btn addTarget:self action:@selector(btn_Click:) forControlEvents:UIControlEventTouchUpInside];
23         [self.view addSubview:btn];
24     }
25     
26     UIImageView *iv=[[UIImageView alloc] initWithFrame:CGRectMake(100, 400, 200, 200)];
27     iv.tag=100;
28     [self.view addSubview:iv ];
29     
30 }
 1 -(void)btn_Click:(UIButton *)sender{
 2     NSInteger i=sender.tag;
 3     switch (i) {
 4         case 1:
 5             //实现拍照
 6             if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
 7                 //相机可用
 8                 [self loadImagePickerWithType:UIImagePickerControllerSourceTypeCamera];
 9             }else{
10                 NSLog(@"拍照功能不可用");
11             }
12             break;
13         case 2:{
14             //实现相册
15             if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
16                 //相册可用
17                 [self loadImagePickerWithType:UIImagePickerControllerSourceTypePhotoLibrary];
18             }else{
19                 NSLog(@"不能获取系统相册");
20             }
21         }
22             break;
23         case 3:{
24             //实现视频播放
25             NSString *strPath=[[NSBundle mainBundle ] pathForResource:@"1" ofType:@".mp4"];
26             [self  playVedio:strPath];
27         }
28             break;
29         default:
30             //
31             break;
32     }
33 }
34 //拍照及相册功能调用的函数(UIImagePickerControllerSourceType是枚举不是类不要*)
35 -(void)loadImagePickerWithType:(UIImagePickerControllerSourceType)type{
36     UIImagePickerController *ipc=[[UIImagePickerController alloc] init];
37     ipc.sourceType=type;
38     ipc.delegate=self;
39     //如果是相册功能,设置允许编辑图片
40     ipc.allowsEditing=YES;
41     [self presentViewController:ipc animated:NO completion:nil];
42 }
43 -(void)playVedio:(NSString *)path{
44     //实现视频文件的播放
45     if (path.length==0) {
46         NSLog(@"请输入有效的文件路径");
47         return;
48     }
49     //判断文件是本地文件还是远程文件
50     NSURL *URL;
51     if ([path rangeOfString:@"http://"].location!=NSNotFound || [path rangeOfString:@"http:"].location!=NSNotFound) {
52         //本地文件
53         URL=[NSURL URLWithString:path];
54     }else{
55         
56         URL=[NSURL fileURLWithPath:path];
57     }
58     //创建视频播放对象
59     MPMoviePlayerViewController *mpvc=[[MPMoviePlayerViewController alloc] initWithContentURL:URL];
60     //设置播放类型
61     mpvc.moviePlayer.movieSourceType=MPMovieSourceTypeFile;
62     //显示播放窗口
63     [self presentViewController:mpvc animated:NO completion:nil];
64     //播放文件
65     [[mpvc moviePlayer] play];
66 }
67 #pragma mark - 实现拍照及相册功能的协议函数
68 -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
69     [picker dismissViewControllerAnimated:NO completion:nil];
70 }
71 -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
72     //在此函数里获取通过相册选择的图片
73     NSString *type=info[UIImagePickerControllerMediaType];
74     //需要引入一个系统的常量类,定义了各种常量
75     if ([type isEqualToString:(NSString *)kUTTypeImage]) {
76         //获取图片,对图片进行处理
77         UIImage *image=info[UIImagePickerControllerEditedImage];
78         UIImageView *iv=(UIImageView *)[self.view viewWithTag:100];
79         iv.image=image;
80     }
81     [picker dismissViewControllerAnimated:NO completion:nil];
82 }
原文地址:https://www.cnblogs.com/crushing/p/4820295.html