#import "ViewController.h"
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>
{
CGFloat _kWidth;
CGFloat _kHeight;
UIImagePickerController *_imagePickerController;/**< 图片拾取控制器,课通过该类访问相册,拍照和录影,要使用图片拾取控制器,需要同时遵循 UINavigationControllerDelegate和UIImagePickerControllerDelegate 协议代理*/
}
@property (nonatomic, strong) UIImageView *imageview;
@property (nonatomic, strong) UIButton *button;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initalizeDataSource];
[self initalizeUserInterface];
}
- (void)initalizeDataSource{ //初始化数据源
_imagePickerController = [[UIImagePickerController alloc]init];
_imagePickerController.delegate = self;
_kWidth = [UIScreen mainScreen].bounds.size.width;
_kHeight = [UIScreen mainScreen].bounds.size.height;
}
- (void)initalizeUserInterface{ //初始化用户界面
[self.view addSubview:self.imageview];
[self.view addSubview:self.button];
}
#pragma mark -- private methods
//相册
- (void)accessPhotoALbm{
//1、获取支持的媒体格式
NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
//2、判断是否支持的sourceType
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
//3、设置sourceType
_imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//4、设置媒体格式
NSLog(@"%@",mediaTypes);
_imagePickerController.mediaTypes = @[mediaTypes.firstObject];
//5、设置是否允许编辑 如果该属性为NO 就不会进入到照片的编辑界面
_imagePickerController.allowsEditing = YES;
//6、推送
[self presentViewController:_imagePickerController animated:YES completion:nil];
}else{
NSLog(@"设备不支持访问相册");
}
}
//拍照
- (void)takingPictures{
//1、获取支持的媒体格式
NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
//2、判断是否支持的sourceType
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//3、设置sourceType
_imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;/**< 数组内的第一个元素是照相,第二个是录像 */
//4、设置媒体格式
NSLog(@"%@",mediaTypes);
_imagePickerController.mediaTypes = @[mediaTypes.firstObject];
//5、设置相机模式
_imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
//6、设置摄像头(前置、后置)
_imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
//7、设置闪光灯
_imagePickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
//8、推送
[self presentViewController:_imagePickerController animated:YES completion:nil];
}else{
NSLog(@"设备不支持访问拍照");
}
}
//录影
- (void)takingVideo{
//1、获取支持的媒体格式
NSArray *mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
//2、判断是否支持的sourceType
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
//3、设置sourceType
_imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;/**< 数组内的第一个元素是照相,第二个是录像 */
//4、设置媒体格式
NSLog(@"%@",mediaTypes);
_imagePickerController.mediaTypes = @[mediaTypes.lastObject];
//5、设置相机模式
_imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
//6、设置摄像头(前置、后置)
_imagePickerController.cameraDevice = UIImagePickerControllerCameraDeviceFront;
//7、设置闪光灯
_imagePickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeAuto;
//8、推送
[self presentViewController:_imagePickerController animated:YES completion:nil];
}else{
NSLog(@"设备不支持访问拍照");
}
}
#pragma mark -- UIImagePickerControllerDelegate 协议方法
//用户选择取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[picker dismissViewControllerAnimated:YES completion:nil];
}
//用户选择某个媒体
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
NSLog(@"%@",info);
UIImage *image = info[UIImagePickerControllerOriginalImage];
//赋值
self.imageview.image = image;
[picker dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark -- 点击事件
- (void)action_button:(UIButton *)sender{
//弹窗提示用户
UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"上传头像" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[alertController addAction:[UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//进入相册
[self accessPhotoALbm];
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self takingPictures];
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"录影" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[self takingVideo];
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
}]];
[self presentViewController:alertController animated:YES completion:nil];
}
#pragma mark -- Getters
- (UIImageView *)imageview{
if (!_imageview){
_imageview = [[UIImageView alloc]initWithFrame:CGRectMake(_kWidth * 0.125 , _kHeight * 0.1, _kWidth * 0.75, _kHeight * 0.3)];
_imageview.backgroundColor = [UIColor redColor];
//设置图片的拉伸模式
_imageview.contentMode = UIViewContentModeScaleAspectFit;
}
return _imageview;
}
- (UIButton *)button{
if (!_button){
_button = [UIButton buttonWithType:UIButtonTypeSystem];
_button.frame = CGRectMake(_kWidth * 0.25, _kHeight * 0.5, _kWidth * 0.5, _kHeight * 0.05);
[_button setTitle:@"选择图片" forState:UIControlStateNormal];
_button.titleLabel.font = [UIFont systemFontOfSize:20];
[_button addTarget:self action:@selector(action_button:) forControlEvents:UIControlEventTouchUpInside];
}
return _button;
}
@end
相机、相册、录影
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1.打开plist-右键-open as - source code,在里面找位置添加 2.列举的里面可能不全的,...