//横屏方法:

AppDelegate.m 中添加监听

//监听页面翻转

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setOrientationPortrait:) name:KNotification_setOrientationPortrait object:nil];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(setOrientationLandscape:) name:KNotification_setOrientationLandscape object:nil];

#pragma mark - UIInterfaceOrientation

- (UIInterfaceOrientationMask)application:(UIApplication*)application supportedInterfaceOrientationsForWindow:(UIWindow*)window{

// iPhone doesn't support upside down by default, while the iPad does.  Override to allow all orientations always, and let the root view controller decide what's allowed (the supported orientations mask gets intersected).

if (!_isPortrait) {

return UIInterfaceOrientationMaskAll;

}

return UIInterfaceOrientationMaskPortrait;

}

- (void)setOrientationPortrait:(NSNotification *)aNotification{

NSLog(@"Portrait");

_isPortrait = YES;

}

- (void)setOrientationLandscape:(NSNotification *)aNotification{

NSLog(@"Landscape");

_isPortrait = NO;

if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {

SEL selector = NSSelectorFromString(@"setOrientation:");

NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];

[invocation setSelector:selector];

[invocation setTarget:[UIDevice currentDevice]];

int val =UIInterfaceOrientationPortrait;

[invocation setArgument:&val atIndex:2];

[invocation invoke];

}

}

在需要横屏的时候

- (void)btnClicked{

CustomImagePickerController *aCustomImagePickerController = [[CustomImagePickerController alloc]init];

[[NSNotificationCenter defaultCenter]postNotificationName:KNotification_setOrientationLandscape object:nil];

[self presentViewController:aCustomImagePickerController animated:YES completion:nil];

}

//实现session自定义相机代码转自网络

//

//  CustomImagePickerController.h

//

//  Created by SoldierTime on 15/10/22.

//  Copyright © 2015年 HeLi. All rights reserved.

#import <UIKit/UIKit.h>

#import <AVFoundation/AVFoundation.h>

@class CustomImagePickerController;

@protocol CustomImagePickerControllerDelegate <NSObject>

- (void)customImagePickerController:(CustomImagePickerController *)picker didFinishPickingImage:(UIImage *)image;

@end

@interface CustomImagePickerController : UIViewController

@property(nonatomic)NSUInteger orietation;

@property (weak ,nonatomic) id <CustomImagePickerControllerDelegate> delegate;

@property (nonatomic, strong)       AVCaptureSession            * session;

//AVCaptureSession对象来执行输入设备和输出设备之间的数据传递

@property (nonatomic, strong)       AVCaptureDeviceInput        * videoInput;

//AVCaptureDeviceInput对象是输入流

@property (nonatomic, strong)       AVCaptureStillImageOutput   * stillImageOutput;

//照片输出流对象,当然我的照相机只有拍照功能,所以只需要这个对象就够了

@property (nonatomic, strong)       AVCaptureVideoPreviewLayer  * previewLayer;

//预览图层,来显示照相机拍摄到的画面

@property (nonatomic, strong)       UIBarButtonItem             * toggleButton;

//切换前后镜头的按钮

@property (nonatomic, strong)       UIButton                    * shutterButton;

//拍照按钮

@property (nonatomic, strong)       UIButton                    * cancelButton;

//取消按钮

@property (nonatomic, strong)       UIButton                    * doneButton;

//使用按钮

@property (nonatomic, strong)       UIView                      * cameraShowView;

//放置预览图层的View

@property (nonatomic, strong)       UIImageView                 * resultImageView;

//放置结果图层的View

@end

//

//  CustomImagePickerController.m

//

//  Created by SoldierTime on 15/10/22.

//  Copyright © 2015年 HeLi. All rights reserved.

//

#import "CustomImagePickerController.h"

@interface CustomImagePickerController ()<UIImagePickerControllerDelegate>

@end

@implementation CustomImagePickerController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

[self initialSession];

//放置预览图层的View

_cameraShowView = [[UIView alloc]init];

_cameraShowView.frame = CGRectMake(0, 0 ,DTSCREEN_HEIGHT, DTSCREEN_WIDTH);

[self.view addSubview:_cameraShowView];

NSLog(@"%f %f",DTSCREEN_HEIGHT,DTSCREEN_WIDTH);

//显示拍照结果

_resultImageView = [[UIImageView alloc]init];

_resultImageView.frame = CGRectMake(0, 0, 192, 108);

[self.view addSubview:_resultImageView];

//拍照按钮

self.shutterButton = [UIButton buttonWithType:0];

self.shutterButton.frame = CGRectMake(50, 200, 50, 50);

self.shutterButton.backgroundColor = DTRGBACOLOR(100, 100, 100, 0.2);

[self.view addSubview:self.shutterButton];

[self.shutterButton addTarget:self action:@selector(shutterCamera) forControlEvents:UIControlEventTouchUpInside];

//使用按钮

self.doneButton = [UIButton buttonWithType:0];

self.doneButton.frame = CGRectMake(150, 200, 50, 50);

self.doneButton.backgroundColor = DTRGBACOLOR(0, 100, 0, 0.2);

[self.view addSubview:self.doneButton];

[self.doneButton addTarget:self action:@selector(finishPicking) forControlEvents:UIControlEventTouchUpInside];

//取消按钮

self.cancelButton = [UIButton buttonWithType:0];

self.cancelButton.frame = CGRectMake(250, 200, 50, 50);

self.cancelButton.backgroundColor = DTRGBACOLOR(100, 0, 0, 0.2);

[self.view addSubview:self.cancelButton];

[self.cancelButton addTarget:self action:@selector(closeView) forControlEvents:UIControlEventTouchUpInside];

}

- (void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:animated];

[self setUpCameraLayer];

}

- (void) viewDidAppear:(BOOL)animated{

[super viewDidAppear:animated];

if (self.session) {

[self.session startRunning];

}

}

- (void)viewWillDisappear:(BOOL)animated{

[super viewWillDisappear:animated];

}

- (void) viewDidDisappear:(BOOL)animated{

[super viewDidDisappear: animated];

if (self.session) {

[self.session stopRunning];

}

}

- (void) initialSession{

//这个方法的执行我放在init方法里了

self.session = [[AVCaptureSession alloc] init];

self.videoInput = [[AVCaptureDeviceInput alloc] initWithDevice:[self backCamera] error:nil];

//    [self fronCamera]方法会返回一个AVCaptureDevice对象,因为我初始化时是采用前摄像头,所以这么写,具体的实现方法后面会介绍

self.stillImageOutput = [[AVCaptureStillImageOutput alloc] init];

NSDictionary * outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey, nil];

//这是输出流的设置参数AVVideoCodecJPEG参数表示以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];

}

AVCaptureConnection *output2VideoConnection = [self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo];

output2VideoConnection.videoOrientation = [self videoOrientationFromCurrentDeviceOrientation];

}

- (void) setUpCameraLayer{

if (self.previewLayer == nil) {

self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.session];

UIView * view = self.cameraShowView;

CALayer * viewLayer = [view layer];

[viewLayer setMasksToBounds:YES];

[self.previewLayer setFrame:CGRectMake(0, 0, DTSCREEN_WIDTH, DTSCREEN_HEIGHT)];

[self.previewLayer setVideoGravity:AVLayerVideoGravityResizeAspect];

self.previewLayer.connection.videoOrientation = [self videoOrientationFromCurrentDeviceOrientation];

[viewLayer insertSublayer:self.previewLayer below:[[viewLayer sublayers] objectAtIndex:0]];

}

}

- (AVCaptureVideoOrientation) videoOrientationFromCurrentDeviceOrientation {

switch (self.interfaceOrientation) {

case UIInterfaceOrientationPortrait: {

return AVCaptureVideoOrientationPortrait;

}

case UIInterfaceOrientationLandscapeLeft: {

return AVCaptureVideoOrientationLandscapeLeft;

}

case UIInterfaceOrientationLandscapeRight: {

return AVCaptureVideoOrientationLandscapeRight;

}

case UIInterfaceOrientationPortraitUpsideDown: {

return AVCaptureVideoOrientationPortraitUpsideDown;

}

}

return AVCaptureVideoOrientationLandscapeLeft;

}

- (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];

}

#pragma mark - actions

- (void)closeView{

[[NSNotificationCenter defaultCenter]postNotificationName:@"setOrientationPortrait" object:nil];

[self dismissViewControllerAnimated:YES completion:^{

}];

}

- (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(@"toggle carema failed, error = %@", error);

}

}

}

- (void) shutterCamera{

AVCaptureConnection * videoConnection = [self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo];

if (!videoConnection) {

NSLog(@"take photo failed!");

return;

}

[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {

if (imageDataSampleBuffer == NULL) {

return;

}

NSData * imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];

UIImage * image = [UIImage imageWithData:imageData];

self.resultImageView.image = image;

NSLog(@"image size = %@",NSStringFromCGSize(image.size));

}];

}

- (void)finishPicking{

if (_delegate) {

[_delegate customImagePickerController:self didFinishPickingImage:self.resultImageView.image];

}

[self closeView];

}

#pragma mark Orientations

- (BOOL)shouldAutorotate{

return NO;

}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations{

return UIInterfaceOrientationMaskLandscape;

}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{

return (interfaceOrientation != UIInterfaceOrientationLandscapeLeft

|| interfaceOrientation != UIInterfaceOrientationLandscapeRight);

}

- (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

iOS 整体项目竖屏 相机横屏相关推荐

  1. iOS 项目整体是竖屏,个别页面支持横屏

    最近在做一个视频APP,工程整体是竖屏的,如下图,播放器需支持横屏.在转屏的过程遇到很痛苦的问题.[UIApplication sharedApplication].statusBarOrientat ...

  2. 剪辑视频,教你将视频竖屏改横屏播放

    剪辑的视频会发布在不同平台,而每个平台的要求都不一样,有些平台要求竖屏,有些平台可能使用横屏更合适,那么我们该如何将竖屏改为横屏呢?不会操作的朋友接着往下看吧,小编演示具体的操作步骤. 开始操作之前先 ...

  3. 开机动画完毕后出现竖屏转横屏的现象

    开机动画完毕后出现竖屏转横屏的现象 开机后第一帧是竖屏,log: Line 4531: 05-25 03:49:50.286 814 814 V ViewRootImpl[FallbackHome]: ...

  4. 如何将竖屏改横屏,并裁切多段视频画面

    在剪辑过程中,该如何将多段竖屏视频转为横屏,并裁切画面呢?今天小编给分享一个新的技巧,下面一起来试试. 材料准备: 一台Win系统电脑 视频剪辑高手 多段视频素材 步骤演示: 运行视频剪辑高手,在剪辑 ...

  5. android竖屏固定,ANDROID强制锁定竖屏_APP固定设置竖屏或横屏

    1.XML锁定横屏或竖屏 在没有设置屏幕方向的情况下会默认设置为:android:screenOrientation="unspecified".即未指明屏幕方向.属性取值land ...

  6. php判断显示器横屏还是竖屏,判断横屏竖屏(三种)

    在做移动端页面的时候经常会遇到需要判断横屏还是竖屏.下面将目前已知的通过HTML,CSS,JS三种判断方法记录下来,方便以后翻阅. 1.通过在html中分别引用横屏和竖屏的样式: //引用竖屏的CSS ...

  7. (转)ANDROID强制锁定竖屏_APP固定设置竖屏或横屏

    1.XML锁定横屏或竖屏 在没有设置屏幕方向的情况下会默认设置为:android:screenOrientation="unspecified".即未指明屏幕方向.属性取值land ...

  8. 在H5移动端开发强制竖屏,横屏时提示

    1.不同的浏览器有不同的做法,参考如下: <!-- uc强制竖屏 --> <meta name="screen-orientation" content=&quo ...

  9. h5页面旋转90度竖屏变横屏

    css 代码如下: .trans {position: absolute;top: -100vw;width: 100vh;height: 100vw;background: #F9FBFF;tran ...

最新文章

  1. 深度学习的seq2seq模型——本质是LSTM,训练过程是使得所有样本的p(y1,...,yT‘|x1,...,xT)概率之和最大...
  2. beautifulsoup解析动态页面div未展开_两个资讯爬虫解析库的用法与对比
  3. “中国风”拯救国货彩妆?
  4. 《需求工程——软件建模与分析》阅读笔记二
  5. android 回调函数一:基本概念
  6. 罗马音平假名片假名转换器_记不住五十音的你,你肯定需要这套日语五十音谐音巧记法...
  7. 留言板小程序开发笔记3
  8. windows安全模式_Windows 安全模式的功能和作用
  9. 如何识别一个字符串是否Json格式
  10. SendMessage函数完全使用手册 (转)
  11. 本泽马梅开二度瓦拉内染红 10人皇马4:2客胜西班牙人
  12. Python实现支持人机对战的五子棋软件(超详细)
  13. 汽车车架号识别 VIN码识别,在汽车后市场的应用
  14. java swing 图片gif_Java swing显示gif
  15. android 录屏工具,Android截屏、录屏工具
  16. (C语言)signed和unsigned类型转化
  17. mac php开发套件_Mac 下搭建 PHP 开发环境的步骤
  18. 汉诺塔(河内塔)问题
  19. 小程序动端组件库Vant Weapp教程
  20. 目前大数据分析领域,主要面临的四大瓶颈

热门文章

  1. Java打包后运行jar包报错Caused by: org.springframework.beans.factory.BeanCreationException: Error creating be
  2. 使用Java+SSM框架+JSP开发简单在线电影推荐网 电影推荐系统 豆瓣电影爬虫 基于用户、物品的协同过滤推荐算法 大数据 机器学习 SimpleMovieRecommendOnline
  3. 城市园林类毕业论文文献包含哪些?
  4. 将Zotero会议论文集的参考文献格式改成国标格式
  5. Python中如何获取用户的输入,你一定要知道,学Python必看
  6. 传说中的死机短信,不知是真是假
  7. 路标识别 matlab 聚类算法,基于卷积神经网络的交通路标检测v1.doc
  8. 卫星定位与导航相关知识的整理
  9. 基于CNN-LSTM及深度学习的风电场时空组合预测模型
  10. Python 库大全(下)