CameraView.m 6.52 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
//
//  CameraView.m
//  Lighting
//
//  Created by mac on 16/5/24.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "CameraView.h"
#import "NSObject+UIImagePickerController.h"
#import "CustomUIImagePickerController.h"

@interface CameraView ()
{
    UIImagePickerController *_imagePickerController;
}

@end

@implementation CameraView

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _imagePickerController = [[CustomUIImagePickerController alloc] init];
    _imagePickerController.delegate = self;
    _imagePickerController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    _imagePickerController.allowsEditing = YES;
    [self initView];
    // Do any additional setup after loading the view.
}
-(void)initView
{
    self.subView=[[UIView alloc]init];
    self.subView.bounds=CGRectMake(0, 0, 300, 150);
    self.subView.center=self.view.center;
    self.subView.layer.masksToBounds = YES;
    self.subView.layer.cornerRadius = 10;
    self.subView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.subView];
    
    UIButton *btn1=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 300, 50)];
    [btn1 setTitle:@"拍照" forState:UIControlStateNormal];
    [btn1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    btn1.titleLabel.textAlignment=NSTextAlignmentCenter;
    btn1.tag=101;
    [btn1 addTarget:self action:@selector(selectedBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.subView addSubview:btn1];
    
    UIButton *btn2=[[UIButton alloc]initWithFrame:CGRectMake(0, 50, 300, 50)];
    [btn2 setTitle:@"从相册中选取" forState:UIControlStateNormal];
    [btn2 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    btn2.titleLabel.textAlignment=NSTextAlignmentCenter;
    btn2.tag=102;
    [btn2 addTarget:self action:@selector(selectedBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.subView addSubview:btn2];
    UIButton *btn3=[[UIButton alloc]initWithFrame:CGRectMake(0, 100, 300, 50)];
    [btn3 setTitle:@"取消" forState:UIControlStateNormal];
    [btn3 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    btn3.titleLabel.textAlignment=NSTextAlignmentCenter;
    btn3.tag=103;
    [btn3 addTarget:self action:@selector(selectedBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self.subView addSubview:btn3];

      //点击手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(DismissScreenView:)];
    tap.delegate = self;
    tap.cancelsTouchesInView = NO;
    [[UIApplication sharedApplication].keyWindow addGestureRecognizer:tap];
}
-(void)selectedBtn:(UIButton*)sender
{
    switch (sender.tag) {
        case 101://拍照
        {
76 77

        [self selectImageFromCamera];
78 79 80 81
        }
            break;
        case 102://从相册中选取
        {
82
         [self selectImageFromAlbum];
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
        }
            break;
        case 103://取消
        {
            [self DismissScreenView];
        }
            break;


            
        default:
            break;
    }
}
#pragma mark 从摄像头获取图片或视频
- (void)selectImageFromCamera
{
    _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    
102 103
    //在打开相机之前,设置屏幕为支持竖屏
    [DeviceDirectionManager instance].isHorizontal=YES;
104 105
    [self presentViewController:_imagePickerController animated:YES completion:nil];
}
106
#pragma mark 从相册获取图片
107 108 109 110 111
- (void)selectImageFromAlbum
{
    //NSLog(@"相册");
    _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
112 113 114
    //在打开相册之前,设置屏幕为支持竖屏
    [DeviceDirectionManager instance].isHorizontal=YES;

115 116 117 118 119
    [self presentViewController:_imagePickerController animated:YES completion:nil];
}

//适用获取所有媒体资源,只需判断资源类型
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
120
    
121 122 123 124
    NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
    //判断资源类型
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]){
        //如果是图片
125 126 127 128 129
    self.customImage= info[UIImagePickerControllerEditedImage];
        if ([self.delegate respondsToSelector:@selector(addCustomPictureImage:)]) {
            
            [self.delegate addCustomPictureImage:self.customImage];
        }
130 131
        
    }else{
132 133 134 135
       
    }
    [self dismissViewControllerAnimated:YES completion:nil];
     [DeviceDirectionManager instance].isHorizontal=NO;
136
    }
137 138
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
139
    [self dismissViewControllerAnimated:YES completion:nil];
140 141
    [DeviceDirectionManager instance].isHorizontal=NO;

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
}
#pragma mark 图片保存完毕的回调
- (void) image: (UIImage *) image didFinishSavingWithError:(NSError *) error contextInfo: (void *)contextInf{
    
}

#pragma mark 视频保存完毕的回调
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInf{
    if (error) {
        NSLog(@"保存视频过程中发生错误,错误信息:%@",error.localizedDescription);
    }else{
        NSLog(@"视频保存成功.");
    }
}
//取消按钮
-(void)DismissScreenView
{
    if (self) {
160 161 162
        if ([self.delegate respondsToSelector:@selector(restCamerBtnImage)]) {
            [self.delegate restCamerBtnImage];
        }
163 164 165 166 167 168 169 170 171
        [self.view removeFromSuperview];
    }
}
//取消当前页面点击手势
-(void)DismissScreenView:(UITapGestureRecognizer*)sender{
    CGPoint point = [sender locationInView:self.view];
    if (point.x<self.subView.frame.origin.x || point.x >self.subView.frame.origin.x+self.subView.frame.size.width||point.y<self.subView.frame.origin.y||point.y>self.subView.frame.origin.y+self.subView.frame.size.height) {
        
        if (self) {
172 173 174
            if ([self.delegate respondsToSelector:@selector(restCamerBtnImage)]) {
                [self.delegate restCamerBtnImage];
            }
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
            [self.view removeFromSuperview];
        }
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end