iOS 自定义相机,带水印!
//
// HSZJLCameraViewC.m
// HSZJLCamera
//
// Created by 紫霞大仙 on 16/1/16.
// Copyright © 2016年 Hipal. All rights reserved.
//
#import "HSZJLCameraViewC.h"
#import <AVFoundation/AVFoundation.h>
#import "HSSaveViewController.h"
@interface HSZJLCameraViewC ()
/**
* 相机显示区域
*/
@property (weak, nonatomic) IBOutlet UIView *cameraView;
/**
* 水印区域
*/
@property (weak, nonatomic) IBOutlet UIView *logoView;
/**
* 工具栏
*/
@property (weak, nonatomic) IBOutlet UIView *toolView;
/**
* 设备之间的数据传递 第一个创建
*/
@property (nonatomic, strong)AVCaptureSession * session;
/**
* 输入流->笔者认为是相机
*/
@property (nonatomic, strong)AVCaptureDeviceInput * videoInput;
/**
* 照相机
*/
@property (nonatomic, strong)AVCaptureStillImageOutput * stillImageOutput;
/**
* 显示层 (相框) 要第二个创建
*/
@property (nonatomic, strong)AVCaptureVideoPreviewLayer * previewLayer;
/**
* 水印照片
*/
@property (weak, nonatomic) IBOutlet UIImageView *logoImageVIew;
@end
@implementation HSZJLCameraViewC
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
if (floor(NSFoundationVersionNumber) <= NSFoundationVersionNumber_iOS_7_1) {
// iOS 7
[self initSession];
}
else {
// iOS 8
AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
switch (status) {
case AVAuthorizationStatusAuthorized:
[self initSession];
break;
case AVAuthorizationStatusNotDetermined: {
// 用户授权
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
if(granted){
[self initSession];
} else {
[self dismissViewControllerAnimated:YES completion:^{
}];
}
}];
}
break;
default:
break;
}
}
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.clipsToBounds = NO;
self.view.backgroundColor = [UIColor blackColor];
[self setUpCameraLayer];
}
/**
* 初始化
*/
- (void)initSession
{
self.session = [[AVCaptureSession alloc] init];
self.session.sessionPreset = AVCaptureSessionPresetPhoto;
self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self backCamera] error:nil];
self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];
NSDictionary * outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil];
//以JPEG的图片格式输出图片
[self.stillImageOutput setOutputSettings:outputSettings];
if ([self.session canAddInput:self.videoInput]) {
[self.session addInput:self.videoInput];
}
if ([self.session canAddOutput:self.stillImageOutput]) {
[self.session addOutput:self.stillImageOutput];
}
}
/**
* 设置相机layer
*/
- (void) setUpCameraLayer
{
if (self.previewLayer == nil) {
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];
UIView * view = self.cameraView;
CALayer * viewLayer = [view layer];
[viewLayer setMasksToBounds:YES];
CGRect bounds = [view bounds];
bounds.size.width = [UIScreen mainScreen].bounds.size.width;
bounds.size.height = [UIScreen mainScreen].bounds.size.height * 0.6;
[self.previewLayer setFrame:bounds];
[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[viewLayer insertSublayer:self.previewLayer below:[[viewLayer sublayers] objectAtIndex:0]];
}
}
/**
* 拍照
*/
- (void)openCamera{
[self shutterCamera];
}
/**
* 获取前后摄像头对象的方法
*/
- (AVCaptureDevice *)cameraWithPosition:(AVCaptureDevicePosition) position {
NSArray *devices = [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo];
for (AVCaptureDevice *device in devices) {
if ([device position] == position) {
return device;
}
}
return nil;
}
//前置摄像头
- (AVCaptureDevice *)frontCamera {
return [self cameraWithPosition:AVCaptureDevicePositionFront];
}
//后置摄像头
- (AVCaptureDevice *)backCamera {
return [self cameraWithPosition:AVCaptureDevicePositionBack];
}
/**
* 前后摄像头切换
*/
- (void)toggleCamera {
NSUInteger cameraCount = [[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo] count];
if (cameraCount > 1) {
NSError *error;
AVCaptureDeviceInput *newVideoInput;
AVCaptureDevicePosition position = [[_videoInput device] position];
if (position == AVCaptureDevicePositionBack)
newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self frontCamera] error:&error];
else if (position == AVCaptureDevicePositionFront)
newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self backCamera] error:&error];
else
return;
if (newVideoInput != nil) {
[self.session beginConfiguration];
[self.session removeInput:self.videoInput];
if ([self.session canAddInput:newVideoInput]) {
[self.session addInput:newVideoInput];
[self setVideoInput:newVideoInput];
} else {
[self.session addInput:self.videoInput];
}
[self.session commitConfiguration];
} else if (error) {
NSLog(@"打开失败 %@", error);
}
}
}
#pragma mark -- 拍照
- (void) shutterCamera
{
AVCaptureConnection * videoConnection = [self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo];
if (!videoConnection) {
NSLog(@"拍照失败");
return;
}
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
if (imageDataSampleBuffer == NULL) {
return;
}
//照片
NSData * imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
UIImage * image = [UIImage imageWithData:imageData];
HSSaveViewController *saVC = [[HSSaveViewController alloc] initWithNibName:@"HSSaveViewController" bundle:nil];
UIImage *logo = [UIImage imageNamed:@"picture_logo1"];
[self presentViewController:saVC animated:YES completion:^{
[saVC setPhotoImage:image logoImage:logo];
}];
}];
}
- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {
UIGraphicsBeginImageContext(image1.size);
// Draw image1
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
// Draw image2
[image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultingImage;
}
#pragma mark --加水印code
- (UIImage *)completeEditWithImage:(UIImage*)image {
CGSize size = image.size;
UIGraphicsBeginImageContextWithOptions(CGSizeMake(size.width-2, size.height-2), NO, 1.0);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *logo = [UIImage imageNamed:@"picture_logo.png"];
[logo drawAtPoint:CGPointMake(100, 100)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
/**
* 因为在一切低端机 会莫名其妙的内存警告 startRunning -> 映射到屏幕上
*/
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:YES];
if (self.session) {
[self.session startRunning];
}
}
- (void) viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear: animated];
if (self.session) {
[self.session stopRunning];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
NSLog(@"相机内存警告");
}
- (IBAction)takePictureClick:(id)sender {
[self shutterCamera];
}
- (IBAction)cancelClick:(UIButton *)sender {
if (self.navigationController.viewControllers.count >1) {
[self.navigationController popViewControllerAnimated:YES];
}else{
[self dismissViewControllerAnimated:YES completion:^{
}];
}
}
- (IBAction)changeCameraClick:(UIButton *)sender {
[self toggleCamera];
}
- (IBAction)choseLogo:(UIButton*)sender {
switch (sender.tag) {
case 0:
{
//不选
self.logoImageVIew.image = nil;
}
break;
case 1:
{
self.logoImageVIew.image = [UIImage imageNamed:@"picture_logo"];
}
break;
case 2:
{
self.logoImageVIew.image = [UIImage imageNamed:@"picture_logo1"];
}
break;
case 3:
{
self.logoImageVIew.image = [UIImage imageNamed:@"picture_logo"];
}
break;
default:
break;
}
}
@end
转载于:https://www.cnblogs.com/zixiadaxian/p/5137585.html
iOS 自定义相机,带水印!相关推荐
- IOS 自定义相机, 使用 AVFoundation(附实现部分腾讯水印相机功能 demo)
原文链接:http://www.jianshu.com/p/c64bf543f16a 这是一款使用 AVFoundation 自定义的相机,与系统原生相机有一样的外观但比系统的相机更好.更符合实际的开 ...
- iOS 自定义相机功能
导入相机的API在AVFoundation里所以得包含头文件#import <AVFoundation/AVFoundation.h> 1.自定义相机需要的属性 @interface Cu ...
- iOS 自定义相机,拍照旋转
1 , 自定义相机,拿到照片,校正方向 AVCapturePhotoCaptureDelegate 的这个代理方法 func photoOutput(_ output: AVCapturePhotoO ...
- iOS自定义相机界面
先说说今天遇到的几个愚蠢的问题-- 想pop回原来的页 发现连着pop回两页..最后发现是写了两行self.navigationcontroller pop.... 我真是醉了= =.. 补充一点: ...
- iOS自定义相机:带拍摄区域边框、半透明遮罩层、点击屏幕对焦、自动裁剪(含demo源码)
文章目录 前言 I.案例1:加一个长方形的框框并裁剪身份证照片(无半透明遮罩层) 1.1 demo 源码 1.2 控制屏幕旋转方向 1.3 封装富文本API 1.4 设置相机预览层和证件框框的fram ...
- ios 自定义拍照页面_30分钟搞定iOS自定义相机
最近公司的项目中用到了相机,由于不用系统的相机,UI给的相机切图,必须自定义才可以.就花时间简单研究了一下相机的自定义. 相机属于系统硬件,这就需要我们来手动调用iPhone的相机硬件,分为以下步骤: ...
- iOS自定义相机实现拍照和连拍
我们常用的拍照模块,大部分代码都是一样的,甚至都不用改就可以直接拖进项目里面使用.但是这仅仅是简单的相机拍照功能,如果我们希望使用更多的功能,比如说连拍等,就需要多一点处理. 我们还是使用UIImag ...
- iOS - 自定义相机取景框
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];imagePicker.delegate = ...
- iOS自定义相机Demo
// Created by David_Tian on 14-9-24. // Copyright (c) 2014年 All rights reserved. // 未做适配,有兴趣的童鞋可以扩展 ...
最新文章
- SpringBoot配置postgre多数据源(亲测有效!!!)
- 如何查看SharePoint未知错误
- Struts2中jsp page=xxx.action/jsp失效
- Java常见面试题汇总
- 访问数组元素进行赋值
- 居中的文字在小屏幕下后面的换行
- mysql设置user权限允许远程_mysql 用户及权限管理 允许远程连接
- android 正在上传动画,安卓系统上传文件动态显示进度条,进度条经常卡在99%有时也会卡住不显示完整的文字。...
- 计算机视觉—TensorFlow入门(5)
- python递归查找值返回_python – 从树递归中返回值列表
- International Journal of Rock Mechanics and Mining Sciences (Vol 124-12月期最新研究译文)
- 使用Java和eclipse进行XML文件解析20180812_韩信之
- 用C++实现Logo语言的基本命令。
- Unity 接入有道智云AI - 图片翻译
- 微信视频号如何流量变现赚钱呢?
- 零基础的我是这样开始写Python爬虫的(附代码)
- 怎样与团队成员沟通,从而提高团队的执行力?
- 这位年轻人,把CryptoPunks送给了无聊猿公司?B轮4个亿?
- python写的平行四边形_python 已知平行四边形三个点,求第四个点的案例
- 【序列化类Serializer】
热门文章
- python爬虫正则表达式实例-python爬虫 正则表达式解析
- python处理多个excel文件-Python将多个excel文件合并为一个文件
- python画图代码星星-Python中turtle作图示例
- python散点图拟合曲线-【python常用图件绘制#01】线性拟合结果图
- python语言中文社区-python解决中文
- python画折线图详解-python如何画折线图
- 怎么检查python是否安装成功-检查python以及django是否安装配置成功
- python编程 入门到实践-终于懂了python编程从入门到实践
- python有道翻译-Python爬去有道翻译
- java和python的比较-java 和 python的一些对比