IOS開啟系統相機的閃光燈_IOS

來源:互聯網
上載者:User

IOS有兩種的拍照和視頻的方式:

1.直接使用UIImagePickerController,這個類提供了一個簡單便捷的拍照與選擇圖片庫裡圖片的功能。

2.另一種是通過AVFoundation.framework架構完全自訂拍照的介面和選擇圖片庫介面。我只做了第一種,就先給大家介紹第一種做法:

一、首先調用介面前,我們需要先判斷當前裝置是否支援UIImagePickerController,用isSourceTypeAvailable:來判斷是否可用

二、查看符合的媒體類型,這個時候我們調用availableMediaTypeForSourceType:判斷

在調用UIImagePickerController時我們需要加入他的兩個代理方法:

UINavigationControllerDelegate和UIImagePickerControllerDelegate,在調用網路攝影機的時候還可以調閃光燈,一會代碼裡有。

要調用閃光燈需要先建一個AVCaptureSession類的執行個體對象:

複製代碼 代碼如下:

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

在.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];
}

調用相機照片和儲存到圖片庫已經完成。

接著介紹開啟照片庫:

複製代碼 代碼如下:

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

以上所述就是本文的全部內容了,希望大家能夠喜歡。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.