ios簡單實現隱藏拍照界面神器

效果

在這裏插入圖片描述

Code:

//
//  ViewController.m
//  vidio
//
//  Created by bbq on 2019/10/15.
//  Copyright © 2019年 bbq. All rights reserved.
//

#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>//這個框架是選擇媒體類型時需要用到的
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
#define SCREEN_BOUNDS [UIScreen mainScreen].bounds//屏幕尺寸
#define SCREEN_WIDTH CGRectGetWidth([UIScreen mainScreen].bounds)//屏幕寬
#define SCREEN_HEIGHT CGRectGetHeight([UIScreen mainScreen].bounds)//屏幕高
@interface ViewController ()<UINavigationControllerDelegate,UIImagePickerControllerDelegate>{
    BOOL isMovie;//判斷拍照攝像的依據
    UILabel *showTypeLabel;//顯示拍照或是錄像的控件
    UIImageView *showMediaView;//顯示拍照錄像結果的視圖
    UIImage *customImage;
    int doit;
//    typedef NS_ENUM(NSInteger, UIImagePickerControllerCameraFlashMode) {
//        UIImagePickerControllerCameraFlashModeOff  = -1,
//        UIImagePickerControllerCameraFlashModeAuto = 0,
//        UIImagePickerControllerCameraFlashModeOn   = 1
//    };
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UISwitch *mediaSwich = [[UISwitch alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 100, 60, 50, 50)];
    [mediaSwich addTarget:self action:@selector(changeMediaType:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:mediaSwich];
    
    
    showTypeLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(mediaSwich.frame), CGRectGetMinY(mediaSwich.frame)+50, 50, 30)];
    showTypeLabel.textAlignment = NSTextAlignmentCenter;
    showTypeLabel.textColor = [UIColor colorWithRed:0.8963 green:0.3301 blue:1.0 alpha:1.0];
    showTypeLabel.text = @"pic";
    [self.view addSubview:showTypeLabel];
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(100, 100, 100, 100);
    [button setTitle:@"黑屏" forState:UIControlStateNormal];
    button.backgroundColor = [UIColor brownColor];
    [button addTarget:self action:@selector(doit) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    //    showMediaView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 200, 200)];
    //    showMediaView.contentMode = UIViewContentModeScaleAspectFill;
    //    [self.view addSubview:showMediaView];
    
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.frame = CGRectMake(100, 250, 100, 100);
    [button2 setTitle:@"小說" forState:UIControlStateNormal];
    button2.backgroundColor = [UIColor brownColor];
    [button2 addTarget:self action:@selector(doit2) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    //    showMediaView = [[UIImageView alloc]initWithFrame:CGRectMake(100, 200, 200, 200)];
    //    showMediaView.contentMode = UIViewContentModeScaleAspectFill;
    //    [self.view addSubview:showMediaView];
    
    
    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeCustom];
    button3.frame = CGRectMake(100, 400, 100, 100);
    [button3 setTitle:@"自定義" forState:UIControlStateNormal];
    button3.backgroundColor = [UIColor brownColor];
    [button3 addTarget:self action:@selector(doit3) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];
//    showMediaView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
//    showMediaView.contentMode = UIViewContentModeScaleAspectFill;
    //    [self.view addSubview:showMediaView];
    
    UIButton *button4 = [UIButton buttonWithType:UIButtonTypeCustom];
    button4.frame = CGRectMake(120, 520, 50, 50);
    [button4 setTitle:@"select" forState:UIControlStateNormal];
    button4.backgroundColor = [UIColor brownColor];
    [button4 addTarget:self action:@selector(doit4) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button4];
    
    UIButton *button6 = [UIButton buttonWithType:UIButtonTypeCustom];
    button6.frame = CGRectMake(40, 520, 50, 50);
    [button6 setTitle:@"select" forState:UIControlStateNormal];
    button6.backgroundColor = [UIColor brownColor];
    [button6 addTarget:self action:@selector(doit6) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button6];
    
    UIButton *button5 = [UIButton buttonWithType:UIButtonTypeCustom];
    button5.frame = CGRectMake(200, 520, 50, 50);
    [button5 setTitle:@"clean" forState:UIControlStateNormal];
    button5.backgroundColor = [UIColor brownColor];
    [button5 addTarget:self action:@selector(doit5) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button5];
}

-(void)changeMediaType:(UISwitch *)sender{
//    [showTypeLabel rotation];
    showTypeLabel.text = sender.isOn!=YES?@"pic":@"movie" ;
    isMovie = sender.isOn;
    
}

-(void)doits:(NSString *)ns{
    UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
    //選擇攝像頭設備 默認的是選擇相冊
    pickerController.sourceType =  UIImagePickerControllerSourceTypeCamera;
    
    pickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
    
    if (isMovie == YES) {
        //拍照的時候不選擇mediaTypes不會崩潰是因爲默認設置是kUTTypeImage
        //kUTTypeImage包含在MobileCoreServices框架裏面->需要的內容不是OC裏面字符串的類型需要強制轉換
        //錄製視頻類型要選擇kUTTypeMovie 它裏面包含了audio和video
        pickerController.mediaTypes = @[(NSString *)kUTTypeMovie];
        //設置錄像 默認的是拍照 -> 選擇cameraCaptureMode一定要先選擇mediaTypes否則會崩潰
        pickerController.cameraCaptureMode =  UIImagePickerControllerCameraCaptureModeVideo;
    }
    UIImageView *anImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:ns]];
    anImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    //        anImageView.hidden = YES;
    pickerController.cameraOverlayView = anImageView;
    //裏面包含兩個代理
    pickerController.delegate = self;
    
    [self presentViewController:pickerController animated:YES completion:nil];
    
}

-(void)doit{
    doit = 2;
    NSString *strdo1 = @"Watermark3.png";
    [self doits:strdo1];
}

-(void)doit2{
    doit = 2;
    NSString *strdo1 = @"Watermark4.png";
    [self doits:strdo1];
}

-(void)doit3{
    doit = 2;
    UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
    //選擇攝像頭設備 默認的是選擇相冊
    pickerController.sourceType =  UIImagePickerControllerSourceTypeCamera;
    
    pickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
    
    if (isMovie == YES) {
        //拍照的時候不選擇mediaTypes不會崩潰是因爲默認設置是kUTTypeImage
        //kUTTypeImage包含在MobileCoreServices框架裏面->需要的內容不是OC裏面字符串的類型需要強制轉換
        //錄製視頻類型要選擇kUTTypeMovie 它裏面包含了audio和video
        pickerController.mediaTypes = @[(NSString *)kUTTypeMovie];
        //設置錄像 默認的是拍照 -> 選擇cameraCaptureMode一定要先選擇mediaTypes否則會崩潰
        pickerController.cameraCaptureMode =  UIImagePickerControllerCameraCaptureModeVideo;
    }
    
    UIImageView *anImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Watermark.png"]];
    
    UIImage *cusImage= [self getDocumentImage];
    if(cusImage != nil){
        anImageView = [[UIImageView alloc] initWithImage:cusImage];
    }
    
    anImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    //        anImageView.hidden = YES;
    pickerController.cameraOverlayView = anImageView;
    //裏面包含兩個代理
    pickerController.delegate = self;
    
    [self presentViewController:pickerController animated:YES completion:nil];
}

-(void)doit4{
    doit = 1;
    UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
    //選擇攝像頭設備 默認的是選擇相冊
    pickerController.sourceType =  UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    pickerController.delegate = self;
    [self presentViewController:pickerController animated:YES completion:nil];
}

-(void)doit6{
    doit = 1;
    UIImagePickerController *pickerController = [[UIImagePickerController alloc]init];
    //選擇攝像頭設備 默認的是選擇相冊
    pickerController.sourceType =  UIImagePickerControllerSourceTypeCamera;
    pickerController.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
    UIImageView *anImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Watermark.png"]];
   
    anImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    //        anImageView.hidden = YES;
    pickerController.cameraOverlayView = anImageView;
    //裏面包含兩個代理
    pickerController.delegate = self;
    [self presentViewController:pickerController animated:YES completion:nil];
}

-(void)doit5{
//    NSString *path_sandox = NSHomeDirectory();
    //設置一個圖片的存儲路徑
    NSString *path_sandox = NSHomeDirectory();
    NSString *imagePath = [path_sandox stringByAppendingString:@"/Documents/customImage.png"];
    NSFileManager *mgr = [NSFileManager defaultManager];
    [mgr removeItemAtPath:imagePath error:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSLog(@"錄製完成");
    NSLog(@">>:%@",info);
    if ([info[UIImagePickerControllerMediaType]isEqualToString:(NSString *)kUTTypeMovie]) {
        NSLog(@"錄像,錄製完成");
        NSString *path = (NSString *)[info[UIImagePickerControllerMediaURL] path];
        //保存視頻到相冊
        UISaveVideoAtPathToSavedPhotosAlbum(path, self, @selector(video:didFinishSavingWithError:contextInfo:), nil) ;
        
    }
    if ([info[UIImagePickerControllerMediaType]isEqualToString:(NSString *)kUTTypeImage] ) {
        UIImage *finishImage = info[UIImagePickerControllerOriginalImage];
//        showMediaView.image = nil;
//        showMediaView.image = finishImage;
        //將圖片轉換成二進制
        //        NSData *imageData = UIImageJPEGRepresentation(finishImage, 0.1);
        //        NSData *imageData1 = UIImagePNGRepresentation(finishImage);
        //把照片保存到相冊
        if(doit == 1){
            NSLog(@"選擇自定義背景完成");
            NSString *path_sandox = NSHomeDirectory();
            //設置一個圖片的存儲路徑
            NSString *imagePath = [path_sandox stringByAppendingString:@"/Documents/customImage.png"];
            //把圖片直接保存到指定的路徑(同時應該把圖片的路徑imagePath存起來,下次就可以直接用來取)
            [UIImagePNGRepresentation(finishImage) writeToFile:imagePath atomically:YES];
        }else{
            NSLog(@"拍照,拍照完成");
            UIImageWriteToSavedPhotosAlbum(finishImage, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
            
        }
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

// 讀取
-(UIImage *)getDocumentImage{
    // 讀取沙盒路徑圖片
     NSString *path_sandox = NSHomeDirectory();
     NSString *aPath3=[path_sandox stringByAppendingString:@"/Documents/customImage.png"];
    // 拿到沙盒路徑圖片
    UIImage *imgFromUrl3=[[UIImage alloc]initWithContentsOfFile:aPath3];
    NSData *imageData = UIImageJPEGRepresentation(imgFromUrl3,1);
    NSLog(@"有緩存%zu",[imageData length]/1024);
    return imgFromUrl3;
    
}

//視頻保存到相冊之後調用
- (void)video:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
//    AVPlayer *player = [AVPlayer playerWithURL:[NSURL fileURLWithPath:videoPath]];
//    AVPlayerViewController *vc = [[AVPlayerViewController alloc]init];
//    vc.player = player;
//    [self presentViewController:vc animated:YES completion:nil];
    
}
//保存成功就會調用這個方法
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    NSLog(@"保存成功");
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    NSLog(@"取消");
    [self dismissViewControllerAnimated:YES completion:nil];
}


@end

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章