/**
注意点: 1.看 GIF 效果图.
2.看连线视图的效果图.
3.看实现代码(直接复制实现效果).
4.此 dome 必须使用真机才可以测试,模拟器是不支持打开照相机的.
5.在 info.plist 要配置key如图4.
6.如果是4S 手机 可以不加这些 key
*/
一、GIF 效果图:
二、连线视图的效果图:
图1:
图2:
图3:
图4:
/**
注意点:在 info.plist 里面加 key.
1.NSCameraUsageDescription ->相机
2.NSMicrophoneUsageDescription -> 麦克风
3.NSCameraUsageDescription ->相机
*/
三、实现代码:
=========================
===================================================
===================控制器1:
//
// ViewController.m
// 图片选择器~dome
//
// Created by 石虎 on 2017/8/13.
// Copyright © 2017年 shihu. All rights reserved.
//
#import "ViewController.h"
#import
#import
//设置委托协议
@interface ViewController ()<UIImagePickerControllerDelegate,
UINavigationControllerDelegate>
//拍照
- (IBAction)takePhoto:(UIButton *)sender;
//拍摄视频
- (IBAction)shootVideo:(UIButton *)sender;
//选取照片
- (IBAction)selectPhotos:(UIButton *)sender;
//图像选择控制器
@property(nonatomic,strong)UIImagePickerController *picker;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
//初始化图像选择控制器
_picker = [[UIImagePickerControlleralloc]init];
//遵守代理
_picker.delegate =self;
}
#pragma mark - 拍照
- (IBAction)takePhoto:(UIButton *)sender {
// 如果拍摄的摄像头可用
if ([UIImagePickerControllerisSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
// 将sourceType设为UIImagePickerControllerSourceTypeCamera代表拍照或拍视频
_picker.sourceType =UIImagePickerControllerSourceTypeCamera;
// 设置拍摄照片
_picker.cameraCaptureMode =UIImagePickerControllerCameraCaptureModePhoto;
// 设置使用手机的后置摄像头(默认使用后置摄像头)
_picker.cameraDevice =UIImagePickerControllerCameraDeviceRear;
// 设置使用手机的前置摄像头。
//picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
// 设置拍摄的照片允许编辑
_picker.allowsEditing =YES;
}else{
NSLog(@"模拟器无法打开摄像头");
}
// 显示picker视图控制器
[selfpresentViewController:_pickeranimated:YEScompletion:nil];
}
#pragma mark - 拍摄视频
- (IBAction)shootVideo:(UIButton *)sender {
// 如果拍摄的摄像头可用
if ([UIImagePickerControllerisSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
// 将sourceType设为UIImagePickerControllerSourceTypeCamera代表拍照或拍视频
_picker.sourceType =UIImagePickerControllerSourceTypeCamera;
// 将mediaTypes设为所有支持的多媒体类型
_picker.mediaTypes = [UIImagePickerController
availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera];
// 设置拍摄视频
_picker.cameraCaptureMode =UIImagePickerControllerCameraCaptureModeVideo;
// 设置拍摄高质量的视频
_picker.videoQuality =UIImagePickerControllerQualityTypeHigh;
}else {
NSLog(@"模拟器无法打开摄像头");
}
// 显示picker视图控制器
[selfpresentViewController:_pickeranimated:YEScompletion:nil];
}
#pragma mark - 选取照片
- (IBAction)selectPhotos:(UIButton *)sender {
// 设置选择载相册的图片或视频
_picker.sourceType =UIImagePickerControllerSourceTypePhotoLibrary;
//是否允许编辑
_picker.allowsEditing =NO;
// 显示picker视图控制器
[selfpresentViewController:_pickeranimated:YEScompletion:nil];
}
#pragma mark - 图像选择控制器代理实现
// 当得到照片或者视频后,调用该方法
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSLog(@"info--->成功:%@", info);
// 获取用户拍摄的是照片还是视频
NSString * mediaType = [infoobjectForKey:UIImagePickerControllerMediaType];
// 判断获取类型:图片,并且是刚拍摄的照片
if ([mediaTypeisEqualToString:(NSString *)kUTTypeImage]
&& picker.sourceType ==UIImagePickerControllerSourceTypeCamera)
{
UIImage *theImage =nil;
// 判断,图片是否允许修改
if ([pickerallowsEditing])
{
// 获取用户编辑之后的图像
theImage = [info objectForKey:UIImagePickerControllerEditedImage];
}else {
// 获取原始的照片
theImage = [info objectForKey:UIImagePickerControllerOriginalImage];
}
// 保存图片到相册中
UIImageWriteToSavedPhotosAlbum(theImage,self,nil,nil);
}elseif ([mediaTypeisEqualToString:(NSString *)kUTTypeMovie]){//判断获取类型:视频,并且是刚拍摄的视频
//获取视频文件的url
NSURL* mediaURL = [infoobjectForKey:UIImagePickerControllerMediaURL];
//创建ALAssetsLibrary对象并将视频保存到媒体库
ALAssetsLibrary *assetsLibrary = [[ALAssetsLibraryalloc]init];
// 将视频保存到相册中
[assetsLibrary writeVideoAtPathToSavedPhotosAlbum:mediaURL
completionBlock:^(NSURL *assetURL,NSError *error)
{
// 如果没有错误,显示保存成功。
if (!error)
{
NSLog(@"视频保存成功!");
}else {
NSLog(@"保存视频出现错误:%@", error);
}
}];
}
// 隐藏UIImagePickerController
[picker dismissViewControllerAnimated:YEScompletion:nil];
}
#pragma mark - 当用户取消时,调用该方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
NSLog(@"用户取消的拍摄!");
// 隐藏UIImagePickerController
[picker dismissViewControllerAnimated:YEScompletion:nil];
}