IOS打开系统相机的闪光灯

2016-02-19 11:32 20 1 收藏

人生本是一个不断学习的过程,在这个过程中,图老师就是你们的好帮手,下面分享的IOS打开系统相机的闪光灯懂设计的网友们快点来了解吧!

【 tulaoshi.com - 编程语言 】

IOS有两种的拍照和视频的方式:

1.直接使用UIImagePickerController,这个类提供了一个简单便捷的拍照与选择图片库里图片的功能。

2.另一种是通过AVFoundation.framework框架完全自定义拍照的界面和选择图片库界面。我只做了第一种,就先给大家介绍第一种做法:

一、首先调用接口前,我们需要先判断当前设备是否支持UIImagePickerController,用isSourceTypeAvailable:来判断是否可用

二、查看符合的媒体类型,这个时候我们调用availableMediaTypeForSourceType:判断

在调用UIImagePickerController时我们需要加入他的两个代理方法:

UINavigationControllerDelegate和UIImagePickerControllerDelegate,在调用摄像头的时候还可以调闪光灯,一会代码里有。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

要调用闪光灯需要先建一个AVCaptureSession类的实例对象:

代码如下:

//  Created by 张茫原 on 13-1-23.
//  Copyright (c) 2013年 张茫原. All rights reserved.
//
 
#import UIKit/UIKit.h
//调用闪光灯调用框架
#import AVFoundation/AVFoundation.h
 
@interface CameraViewController : UIViewControllerUINavigationControllerDelegate, UIImagePickerControllerDelegate
{
    AVCaptureSession * _AVSession;//调用闪光灯的时候创建的类
}
 
@property(nonatomic,retain)AVCaptureSession * AVSession;
 
@end

在.m的- (void)viewDidLoad里建立4Button,Camera调用相机、Library调用图片库、flashlight打开闪光灯、close关闭闪光灯,这里创建Button的代码我就不再写了。

代码如下:

//打开相机
-(void)addCarema
{
    //判断是否可以打开相机,模拟器此功能无法使用
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        
        UIImagePickerController * picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.allowsEditing = YES;  //是否可编辑
        //摄像头
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentModalViewController:picker animated:YES];
        [picker release];
    }else{
        //如果没有提示用户
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];
        [alert show];
    }
}

打开相机后,然后需要调用UIImagePickerControllerDelegate里的方法,拍摄完成后执行的方法和点击Cancel之后执行的方法:

代码如下:

//拍摄完成后要执行的方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //得到图片
    UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
    //图片存入相册
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    [self dismissModalViewControllerAnimated:YES];
}
//点击Cancel按钮后执行方法
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissModalViewControllerAnimated:YES];
}

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

调用相机照片和保存到图片库已经完成。

接着介绍打开照片库:

代码如下:

-(void)openPicLibrary
{
    //相册是可以用模拟器打开的
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerController * picker = [[UIImagePickerController alloc]init];
        picker.delegate = self;
        picker.allowsEditing = YES;//是否可以编辑
        //打开相册选择照片
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentModalViewController:picker  animated:YES];
        [picker release];
    }else{
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];
        [alert show];
    }
}
//选中图片进入的代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [self dismissModalViewControllerAnimated:YES];
}

调用闪光灯的代码,由于我也不是很理解,所以没法加注释,但是已经亲测可用,但是调闪光灯时有一个算是bug吧,闪光灯会闲一下,然后再一直亮

代码如下:

-(void)openFlashlight
{
    AVCaptureDevice * device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    if (device.torchMode == AVCaptureTorchModeOff) {
        //Create an AV session
        AVCaptureSession * session = [[AVCaptureSession alloc]init];
        // Create device input and add to current session
        AVCaptureDeviceInput * input = [AVCaptureDeviceInput deviceInputWithDevice:device error:nil];
        [session addInput:input];
        // Create video output and add to current session
        AVCaptureVideoDataOutput * output = [[AVCaptureVideoDataOutput alloc]init];
        [session addOutput:output];
        // Start session configuration
        [session beginConfiguration];
        [device lockForConfiguration:nil];
        // Set torch to on
        [device setTorchMode:AVCaptureTorchModeOn];
        [device unlockForConfiguration];
        [session commitConfiguration];
        // Start the session
        [session startRunning];
        // Keep the session around
        [self setAVSession:self.AVSession];
        [output release];
    }
}
-(void)closeFlashlight
{
    [self.AVSession stopRunning];
    [self.AVSession release];
}

以上所述就是本文的全部内容了,希望大家能够喜欢。

来源:https://www.tulaoshi.com/n/20160219/1597829.html

延伸阅读
标签: 摄影技巧
闪光灯既可以成就也可以毁掉一张照片。正确的使用闪光灯就能让你拍出精彩的照片。这篇短文会给你介绍一些使用闪光灯的基本方式。 1. 填充闪光 在户外拍摄时,若被摄体或人物脸部有阴影,最简单的处理方式是使用填充闪光。这种方法正如其字面意思所示,是给阴影区加入少量的人造光。户外拍摄时你只需打开闪光灯,剩下的工作就可以交给相机去做...
标签: 摄影入门
在摄影包里加上1、2支闪光灯可以让你的拍摄拥有无限可能。本文就是一篇针对不同功能闪光灯的完全指导手册。 器材 现在有很多闪光灯生产厂家。有些是可以装在热靴上的闪光灯,有些则是大型的影室灯。这里我们只谈热靴闪光灯。这是一种拥有多种控制和曝光功能,非常便携的闪光灯。 你所需要的是一部数码单反相机和一支原厂热靴闪光灯—&m...
标签: 摄影
什么条件下用闪光灯?什么条件下不用?这对拍照效果来说是很重要的问题   闪光灯既可以成就也可以毁掉一张照片!正确的使用闪光灯就能让你拍出精彩的照片。这篇短文会给你介绍一些使用闪光灯的基本方式。爱摄影,追求完美的帅哥美女要好好看了。 1. 使用柔光罩 柔光罩可以削弱光线的“硬度”。它可以柔化光线,拍...
夜景人像闪光灯   每年接近尾声时,年节气氛随着夜间的灯饰摆设而升温,不少人都会带着相机到各个灯饰景点拍照,留下美丽浪漫的照片。今天Herman老师为我们示范的,是运用Sony推出的HLV-F60M闪灯,为大家示范节庆人像的拍摄手法。 Sony HLV-F60M不仅是一支外接闪灯,它同时也附有持续照明的灯光,这样的设计,虽然也出现其它厂牌...
iphone拍照时打开前置摄像头的闪光灯   这是一款非常神奇的插件,它允许你在使用具有前置摄像头的iPhone、iPod、iPad时打开前置摄像头对应的闪光灯以提升光线不好时的自拍效果。 好吧,TuLaoShi.com其实我标题党了,因为前置摄像头根本没有闪光灯,再厉害的插件也变不出来:) 它的真正效果是通过拍照时大幅提升屏幕的亮度并...

经验教程

470

收藏

20
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部