效果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8c337d2b75fa9fd0f0a0a241b49f66d9.gif)
Code:
#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;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
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];
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];
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];
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.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) {
pickerController.mediaTypes = @[(NSString *)kUTTypeMovie];
pickerController.cameraCaptureMode = UIImagePickerControllerCameraCaptureModeVideo;
}
UIImageView *anImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:ns]];
anImageView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
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) {
pickerController.mediaTypes = @[(NSString *)kUTTypeMovie];
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);
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);
pickerController.cameraOverlayView = anImageView;
pickerController.delegate = self;
[self presentViewController:pickerController animated:YES completion:nil];
}
-(void)doit5{
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];
if(doit == 1){
NSLog(@"选择自定义背景完成");
NSString *path_sandox = NSHomeDirectory();
NSString *imagePath = [path_sandox stringByAppendingString:@"/Documents/customImage.png"];
[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{
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
NSLog(@"保存成功");
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
NSLog(@"取消");
[self dismissViewControllerAnimated:YES completion:nil];
}
@end