//聯(lián)系人:石虎QQ: 1224614774昵稱:嗡嘛呢叭咪哄
/**
注意點(diǎn): 1.看 GIF 效果圖.
2.看連線視圖的效果圖.
3.看實(shí)現(xiàn)代碼(直接復(fù)制實(shí)現(xiàn)效果).
4.此 dome 必須使用真機(jī)才可以測(cè)試,模擬器是不支持打開(kāi)照相機(jī)的.
5.在 info.plist 要配置key如圖4.
6.如果是4S 手機(jī) 可以不加這些 key
7.詳情請(qǐng)看: http://blog.csdn.net/shihuboke/article/details/77150958
*/
一术幔、GIF 效果圖:
二另萤、連線視圖的效果圖:
圖1:
圖2:
圖3:
圖4:
/**
注意點(diǎn):在 info.plist 里面加 key.
1.NSCameraUsageDescription ->相機(jī)
2.NSMicrophoneUsageDescription ->麥克風(fēng)
3.NSCameraUsageDescription ->相機(jī)
*/
三、實(shí)現(xiàn)代碼:
=========================
===================================================
===================
控制器1:
//
//? ViewController.m
//圖片選擇器~dome
//
//? Created by石虎on 2017/8/13.
//? Copyright ? 2017年shihu. All rights reserved.
//
#import"ViewController.h"
#import//資產(chǎn)庫(kù)框架
#import//移動(dòng)核心服務(wù)框架
//設(shè)置委托協(xié)議
@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設(shè)為UIImagePickerControllerSourceTypeCamera代表拍照或拍視頻
_picker.sourceType=UIImagePickerControllerSourceTypeCamera;
//設(shè)置拍攝照片
_picker.cameraCaptureMode=UIImagePickerControllerCameraCaptureModePhoto;
//設(shè)置使用手機(jī)的后置攝像頭(默認(rèn)使用后置攝像頭)
_picker.cameraDevice=UIImagePickerControllerCameraDeviceRear;
//設(shè)置使用手機(jī)的前置攝像頭特愿。
//picker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
//設(shè)置拍攝的照片允許編輯
_picker.allowsEditing=YES;
}else{
NSLog(@"模擬器無(wú)法打開(kāi)攝像頭");
}
//顯示picker視圖控制器
[selfpresentViewController:_pickeranimated:YEScompletion:nil];
}
#pragma mark -拍攝視頻
- (IBAction)shootVideo:(UIButton*)sender {
//如果拍攝的攝像頭可用
if([UIImagePickerControllerisSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
//將sourceType設(shè)為UIImagePickerControllerSourceTypeCamera代表拍照或拍視頻
_picker.sourceType=UIImagePickerControllerSourceTypeCamera;
//將mediaTypes設(shè)為所有支持的多媒體類型
_picker.mediaTypes= [UIImagePickerController
availableMediaTypesForSourceType:
UIImagePickerControllerSourceTypeCamera];
//設(shè)置拍攝視頻
_picker.cameraCaptureMode=UIImagePickerControllerCameraCaptureModeVideo;
//設(shè)置拍攝高質(zhì)量的視頻
_picker.videoQuality=UIImagePickerControllerQualityTypeHigh;
}else{
NSLog(@"模擬器無(wú)法打開(kāi)攝像頭");
}
//顯示picker視圖控制器
[selfpresentViewController:_pickeranimated:YEScompletion:nil];
}
#pragma mark -選取照片
- (IBAction)selectPhotos:(UIButton*)sender {
//設(shè)置選擇載相冊(cè)的圖片或視頻
_picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
//是否允許編輯
_picker.allowsEditing=NO;
//顯示picker視圖控制器
[selfpresentViewController:_pickeranimated:YEScompletion:nil];
}
#pragma mark -圖像選擇控制器代理實(shí)現(xiàn)
//當(dāng)?shù)玫秸掌蛘咭曨l后仲墨,調(diào)用該方法
-(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];
}
//保存圖片到相冊(cè)中
UIImageWriteToSavedPhotosAlbum(theImage,self,nil,nil);
}elseif([mediaTypeisEqualToString:(NSString*)kUTTypeMovie]){//判斷獲取類型:視頻,并且是剛拍攝的視頻
//獲取視頻文件的url
NSURL* mediaURL = [infoobjectForKey:UIImagePickerControllerMediaURL];
//創(chuàng)建ALAssetsLibrary對(duì)象并將視頻保存到媒體庫(kù)
ALAssetsLibrary*assetsLibrary = [[ALAssetsLibraryalloc]init];
//將視頻保存到相冊(cè)中
[assetsLibrarywriteVideoAtPathToSavedPhotosAlbum:mediaURL
completionBlock:^(NSURL*assetURL,NSError*error)
{
//如果沒(méi)有錯(cuò)誤俩由,顯示保存成功毒嫡。
if(!error)
{
NSLog(@"視頻保存成功!");
}else{
NSLog(@"保存視頻出現(xiàn)錯(cuò)誤:%@", error);
}
}];
}
//隱藏UIImagePickerController
[pickerdismissViewControllerAnimated:YEScompletion:nil];
}
#pragma mark -當(dāng)用戶取消時(shí),調(diào)用該方法
- (void)imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
NSLog(@"用戶取消的拍攝兜畸!");
//隱藏UIImagePickerController
[pickerdismissViewControllerAnimated:YEScompletion:nil];
}
@end
=========================
===================================================
===================
謝謝!!!