@font-face{ font-family:"Times New Roman"; } @font-face{ font-family:"宋体"; } @font-face{ font-family:"Calibri"; } p.MsoNormal{ mso-style-name:正文; mso-style-parent:""; margin:0pt; margin-bottom:.0001pt; mso-pagination:none; text-align:justify; text-justify:inter-ideograph; font-family:Calibri; mso-fareast-font-family:宋体; mso-bidi-font-family:'Times New Roman'; font-size:10.5000pt; mso-font-kerning:1.0000pt; } span.msoIns{ mso-style-type:export-only; mso-style-name:""; text-decoration:underline; text-underline:single; color:blue; } span.msoDel{ mso-style-type:export-only; mso-style-name:""; text-decoration:line-through; color:red; } @page{mso-page-border-surround-header:no; mso-page-border-surround-footer:no;}@page Section0{ margin-top:72.0000pt; margin-bottom:72.0000pt; margin-left:90.0000pt; margin-right:90.0000pt; size:595.3000pt 841.9000pt; layout-grid:15.6000pt; } div.Section0{page:Section0;}
//
// ViewController.m
// vidio
//
// Created by bbq on 2019/12/31.
// Copyright © 2019年 bbq. All rights reserved.
//
import "ViewController.h"
import //这个框架是选择媒体类型时需要用到的
import
import
define SCREEN_BOUNDS [UIScreen mainScreen].bounds//屏幕尺寸
define SCREEN_WIDTH CGRectGetWidth([UIScreen mainScreen].bounds)//屏幕宽
define SCREEN_HEIGHT CGRectGetHeight([UIScreen mainScreen].bounds)//屏幕高
@interface ViewController ()
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();
function(){ //外汇返佣 http://www.fx61.com/
NSString *imagePath = [path_sandox stringByAppendingString:@"/Documents/customImage.png"];
NSFileManager *mgr = [NSFileManager defaultManager];
[mgr removeItemAtPath:imagePath error:nil];
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary
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