iOS UIImagePickerController(拍照、拍摄照片、选取照片)~demo

//联系人:石虎QQ: 1224614774昵称:嗡嘛呢叭咪哄

/**

注意点: 1.看 GIF 效果图.

2.看连线视图的效果图.

3.看实现代码(直接复制实现效果).

4.此 dome 必须使用真机才可以测试,模拟器是不支持打开照相机的.

5.在 info.plist 要配置key如图4.

6.如果是4S 手机 可以不加这些 key

7.详情请看: http://blog.csdn.net/shihuboke/article/details/77150958

*/


一、GIF 效果图:

二、连线视图的效果图:

图1:

iOS UIImagePickerController(拍照、拍摄照片、选取照片)~demo_第1张图片

图2:

iOS UIImagePickerController(拍照、拍摄照片、选取照片)~demo_第2张图片

图3:

iOS UIImagePickerController(拍照、拍摄照片、选取照片)~demo_第3张图片

图4:

iOS UIImagePickerController(拍照、拍摄照片、选取照片)~demo_第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//移动核心服务框架

//设置委托协议

@interfaceViewController()

UINavigationControllerDelegate>

//拍照

- (IBAction)takePhoto:(UIButton*)sender;

//拍摄视频

- (IBAction)shootVideo:(UIButton*)sender;

//选取照片

- (IBAction)selectPhotos:(UIButton*)sender;

//图像选择控制器

@property(nonatomic,strong)UIImagePickerController*picker;

@end

@implementationViewController

- (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 = [infoobjectForKey:UIImagePickerControllerEditedImage];

}else{

//获取原始的照片

theImage = [infoobjectForKey:UIImagePickerControllerOriginalImage];

}

//保存图片到相册中

UIImageWriteToSavedPhotosAlbum(theImage,self,nil,nil);

}elseif([mediaTypeisEqualToString:(NSString*)kUTTypeMovie]){//判断获取类型:视频,并且是刚拍摄的视频

//获取视频文件的url

NSURL* mediaURL = [infoobjectForKey:UIImagePickerControllerMediaURL];

//创建ALAssetsLibrary对象并将视频保存到媒体库

ALAssetsLibrary*assetsLibrary = [[ALAssetsLibraryalloc]init];

//将视频保存到相册中

[assetsLibrarywriteVideoAtPathToSavedPhotosAlbum:mediaURL

completionBlock:^(NSURL*assetURL,NSError*error)

{

//如果没有错误,显示保存成功。

if(!error)

{

NSLog(@"视频保存成功!");

}else{

NSLog(@"保存视频出现错误:%@", error);

}

}];

}

//隐藏UIImagePickerController

[pickerdismissViewControllerAnimated:YEScompletion:nil];

}

#pragma mark -当用户取消时,调用该方法

- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker

{

NSLog(@"用户取消的拍摄!");

//隐藏UIImagePickerController

[pickerdismissViewControllerAnimated:YEScompletion:nil];

}

@end

=========================

===================================================

===================

谢谢!!!

你可能感兴趣的:(iOS UIImagePickerController(拍照、拍摄照片、选取照片)~demo)