菜单

iphone开采张开系统相机的闪光灯

2019年5月12日 - www6165com

1、首先调用接口前,大家必要先决断当前设备是或不是帮忙UIImagePickerController,用isSourceTypeAvailable:来推断是或不是可用

//
//  Created by 张茫原 on 13-1-23.
//  Copyright (c) 2013年 张茫原. All rights reserved.
//

#import <UIKit/UIKit.h>
//调用闪光灯调用框架
#import <AVFoundation/AVFoundation.h>

@interface CameraViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
    AVCaptureSession * _AVSession;//调用闪光灯的时候创建的类
}

@property(nonatomic,retain)AVCaptureSession * AVSession;

@end

调用闪光灯的代码,由于自家也不是很领会,所以不得已加注释,可是曾经亲测可用,然则调闪光灯时有贰个总算bug吧,闪光灯会闲一下,然后再一贯亮

随后介绍展开照片库:

//拍摄完成后要执行的方法
-(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];
}
-(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];
}

开荒相机后,然后须求调用UIImagePickerControllerDelegate里的艺术,拍摄成功后实践的不二等秘书诀和点击Cancel之后实行的格局:

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

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

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

2、查看符合的媒体类型,那个时候我们调用availableMediaTypeForSourceType:推断

//打开相机
-(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];
    }
}

明日给我们大快朵颐一下怎么调用iphone的留影功效和开垦闪光灯,有个别代码小编也不太知道,大多是在网络借鉴其余人的。IOS有三种的照相和摄像的艺术:壹.平昔运用UIImagePickerController,那一个类提供了1个大致方便的录像与采纳图片Curry图片的法力。贰.另一种是由此AVFoundation.framework框架完全自定义拍照的分界面和挑选图片库分界面。小编只做了第二种,就先给大家介绍第三种做法:

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

在调用UIImagePickerController时我们供给进入他的多个代理方法:

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

相关文章

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图