效果图 Gif.gif调用摄像头拍摄照片和视频需要在真机上进行演示,不巧我玩儿不转没成功7天超过了10个App什么鬼
点击本地视频按钮,没有视频也需要在真机测试555555
ViewController.m文件
#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
@interface ViewController () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *selectedImageView;
@property (nonatomic, strong) UIImagePickerController *pickerCtrl;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.selectedImageView.layer.masksToBounds = YES;
self.selectedImageView.layer.cornerRadius = 100;
}
#pragma mark - 拍照片
- (IBAction)takePhotoAction:(id)sender {
[self takePhoto2];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePickerCtrl = [[UIImagePickerController alloc] init];
imagePickerCtrl.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerCtrl.delegate = self;
imagePickerCtrl.mediaTypes = [NSArray arrayWithObjects:(__bridge_transfer NSString *)kUTTypeImage, nil];
imagePickerCtrl.allowsEditing = YES;
[self presentViewController:imagePickerCtrl animated:YES completion:nil];
}
}
#pragma mark - 拍视频
- (IBAction)takeVideoAction:(id)sender {
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePickerCtrl = [[UIImagePickerController alloc] init];
imagePickerCtrl.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerCtrl.delegate = self;
imagePickerCtrl.mediaTypes = [NSArray arrayWithObjects:(__bridge_transfer NSString *)kUTTypeMovie, nil];
imagePickerCtrl.allowsEditing = YES;
[self presentViewController:imagePickerCtrl animated:YES completion:nil];
}
}
#pragma mark - 显示图片
- (IBAction)showPhotoAction:(id)sender {
UIImagePickerController *imagePickerCtrl = [[UIImagePickerController alloc] init];
imagePickerCtrl.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePickerCtrl.delegate = self;
imagePickerCtrl.mediaTypes = [NSArray arrayWithObjects:(__bridge_transfer NSString *)kUTTypeImage, nil];
imagePickerCtrl.allowsEditing = YES;
[self presentViewController:imagePickerCtrl animated:YES completion:nil];
}
#pragma mark - 显示视频
- (IBAction)showVideoAction:(id)sender {
UIImagePickerController *videoPickerCtrl = [[UIImagePickerController alloc] init];
videoPickerCtrl.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
videoPickerCtrl.delegate = self;
videoPickerCtrl.mediaTypes = [NSArray arrayWithObjects:(__bridge_transfer NSString *)kUTTypeMovie, nil];
videoPickerCtrl.allowsEditing = YES;
[self presentViewController:videoPickerCtrl animated:YES completion:nil];
}
#pragma mark - 自定义拍照
- (void)takePhoto2
{
UIImagePickerController *pickerCtrl = [[UIImagePickerController alloc] init];
pickerCtrl.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
pickerCtrl.delegate = self;
pickerCtrl.mediaTypes = [NSArray arrayWithObjects:(__bridge_transfer NSString *)kUTTypeMovie, nil];
pickerCtrl.allowsEditing = YES;
pickerCtrl.showsCameraControls = NO;
pickerCtrl.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
pickerCtrl.cameraDevice = UIImagePickerControllerCameraDeviceFront;//前置两千万柔光双摄照亮你妹!
pickerCtrl.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;//闪光灯自动~
UIView *ctrlView = [[UIView alloc] initWithFrame:self.view.bounds];
UIButton *captureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
[captureBtn setTitle:@"takePhoto" forState:UIControlStateNormal];
[captureBtn setFrame:CGRectMake(CGRectGetWidth(self.view.bounds) - 120, CGRectGetHeight(self.view.bounds) - 50.f - 30.f, 120, 5)];
[captureBtn setBackgroundColor:[UIColor grayColor]];
[captureBtn addTarget:self action:@selector(captureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
[ctrlView addSubview:captureBtn];
pickerCtrl.cameraOverlayView = ctrlView;
self.pickerCtrl = pickerCtrl;
[self presentViewController:pickerCtrl animated:YES completion:nil];
}
#pragma mark - captureBtnAction点击事件
/**
* 对当前显示的摄像头画面进行拍照:
*/
- (void)captureBtnAction:(id)sender {
[self.pickerCtrl takePicture];
}
#pragma mark - 自定义视频
- (void)takeVideoAction2
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *imagePickerCtrl = [[UIImagePickerController alloc] init];
imagePickerCtrl.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerCtrl.delegate = self;
imagePickerCtrl.mediaTypes = [NSArray arrayWithObjects:(__bridge_transfer NSString *)kUTTypeMovie, nil];
imagePickerCtrl.allowsEditing = YES;
imagePickerCtrl.videoMaximumDuration = 0.5;
//首次打开默认进入录制视频画面:
imagePickerCtrl.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
[self presentViewController:imagePickerCtrl animated:YES completion:nil];
}
}
#pragma mark - <UIImagePickerControllerDelegate>
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {
//字典包含了当前选中的数据:
//选择图片,当我点击choose之后就会跳转到这里 , 而此时很显然我点选的那张图片就保存在了 info 字典里!!!
if (info[UIImagePickerControllerMediaType] == (__bridge_transfer NSString *)kUTTypeImage)
{
UIImage *editImage = info[UIImagePickerControllerEditedImage];
UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
[self.selectedImageView setImage:editImage];
//如果资源类型是照相机类型:
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
//就把拍摄的照片保存到系统相册中 , iOS默认不保存 , 所以我要在这个代理方法里让他保存到photoAlbum里!
UIImageWriteToSavedPhotosAlbum(originalImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
//拿到选取的视频路径:
NSString *videoPath = info[UIImagePickerControllerMediaURL];
//保存视频:
UISaveVideoAtPathToSavedPhotosAlbum(videoPath, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
}
}
else if (info[UIImagePickerControllerMediaType] == (__bridge_transfer NSString *)kUTTypeMovie)
{
//处理相应的视频内容并进行展示:
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
NSLog(@"取消");
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - saved Photo
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error) {
NSLog(@"save photo error");
} else {
NSLog(@"save photo success");
}
}
#pragma mark - saved Video
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error) {
NSLog(@"save video error");
} else {
NSLog(@"save video success");
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end
网友评论