CameraView.m 8.81 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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
//
//  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;
//    [_imagePickerController supportedInterfaceOrientations];
    [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://拍照
        {
//            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
//            if ([UIImagePickerController isSourceTypeAvailable:
//                              UIImagePickerControllerSourceTypePhotoLibrary]) {
//                      imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
//                     imagePicker.delegate = self;
//                     [imagePicker setAllowsEditing:YES];
//                     UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
//                      self.popoverController = popover;
//                [self presentViewController:imagePicker animated:YES completion:nil];
////                    [self.popoverController presentPopoverFromRect:CGRectMake(0, 0, 800, 500) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
//                
//                 }
//            [self selectImageFromAlbum];
        }
            break;
        case 102://从相册中选取
        {
        
        }
            break;
        case 103://取消
        {
            [self DismissScreenView];
        }
            break;


            
        default:
            break;
    }
}
#pragma mark 从摄像头获取图片或视频
- (void)selectImageFromCamera
{
    _imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    //录制视频时长,默认10s
    _imagePickerController.videoMaximumDuration = 15;
    
    //相机类型(拍照、录像...)字符串需要做相应的类型转换
    _imagePickerController.mediaTypes = @[(NSString *)kUTTypeMovie,(NSString *)kUTTypeImage];
    
    //视频上传质量
    //UIImagePickerControllerQualityTypeHigh高清
    //UIImagePickerControllerQualityTypeMedium中等质量
    //UIImagePickerControllerQualityTypeLow低质量
    //UIImagePickerControllerQualityType640x480
    _imagePickerController.videoQuality = UIImagePickerControllerQualityTypeHigh;
    
    //设置摄像头模式(拍照,录制视频)为录像模式
    _imagePickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
    [self presentViewController:_imagePickerController animated:YES completion:nil];
}
#pragma mark 从相册获取图片或视频
- (void)selectImageFromAlbum
{
    //NSLog(@"相册");
    _imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:_imagePickerController animated:YES completion:nil];
}
#pragma mark UIImagePickerControllerDelegate
//该代理方法仅适用于只选取图片时
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo {
    NSLog(@"选择完毕----image:%@-----info:%@",image,editingInfo);
}

//适用获取所有媒体资源,只需判断资源类型
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
    //判断资源类型
    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]){
        //如果是图片
//        self.imageView.image = info[UIImagePickerControllerEditedImage];
//        //压缩图片
//        NSData *fileData = UIImageJPEGRepresentation(self.imageView.image, 1.0);
//        //保存图片至相册
//        UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
//        //上传图片
//        [self uploadImageWithData:fileData];
        
    }else{
        //如果是视频
        NSURL *url = info[UIImagePickerControllerMediaURL];
        //播放视频
//        _moviePlayer.contentURL = url;
//        [_moviePlayer play];
        //保存视频至相册(异步线程)
        NSString *urlStr = [url path];
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(urlStr)) {
                
                UISaveVideoAtPathToSavedPhotosAlbum(urlStr, self, @selector(video:didFinishSavingWithError:contextInfo:), nil);
            }
        });
        NSData *videoData = [NSData dataWithContentsOfURL:url];
        //视频上传
//        [self uploadVideoWithData:videoData];
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}
#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(@"视频保存成功.");
    }
}
//支持横屏
-(NSUInteger)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskLandscape;
}
//取消按钮
-(void)DismissScreenView
{
    if (self) {
        [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) {
            [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