iOS Open System camera flash

Source: Internet
Author: User
Tags uikit

Today to share with you how to call the iphone's camera function and turn on the flash, some code I do not understand, many are on the internet to learn from others.

iOS has two ways of photographing and video:

1. Direct use of Uiimagepickercontroller, this class provides a simple and easy to take pictures and select pictures in the library function.

2. Another way is to fully customize the photo-taking interface through the Avfoundation.framework framework and select the image Library interface. I have only done the first one, first to introduce you to the first approach:

First, before calling the interface, we need to first determine whether the current device supports Uiimagepickercontroller and use issourcetypeavailable: to determine whether it is available

Second, to view the type of media, this time we call Availablemediatypeforsourcetype: Judge

We need to add his two proxy methods when calling Uiimagepickercontroller:

Uinavigationcontrollerdelegate and Uiimagepickercontrollerdelegate, when calling the camera, can also adjust the flash, one will be in the code.

To invoke the flash you need to first build an instance object for the Avcapturesession class:

The code is as follows:

Created by Zhang 13-1-23 on the original.

Copyright (c) 2013 Zhang Yuan. All rights reserved.

//

#import

Invoke the Flash call frame

#import

@interface Cameraviewcontroller:uiviewcontroller

{

Avcapturesession * _avsession;//The class created when the Flash is invoked

}

@property (nonatomic,retain) avcapturesession * avsession;

@end

Create a 4button,camera call camera, library call picture Library, flashlight turn on flash, close turn off the flash in the. M's-(void) Viewdidload, where the button's code is created I don't write anymore.

The code is as follows:

Turn on the camera

-(void) Addcarema

{

To determine if the camera can be turned on, the emulator does not use this feature

if ([Uiimagepickercontroller Issourcetypeavailable:uiimagepickercontrollersourcetypecamera]) {

Uiimagepickercontroller * Picker = [[Uiimagepickercontroller alloc]init];

Picker.delegate = self;

picker.allowsediting = YES; Whether to edit

Camera

Picker.sourcetype = Uiimagepickercontrollersourcetypecamera;

[Self presentmodalviewcontroller:picker animated:yes];

[Picker release];

}else{

If the user is not prompted

Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Error" message:@ "You don't have a webcam delegate:nil cancelbuttontitle:@" Drat! "Otherbuttontitles:nil];

[Alert show];

}

}

After you open the camera, you need to call the method in Uiimagepickercontrollerdelegate, shoot the method that is executed after the completion, and then follow the method after you click Cancel:

The code is as follows:

How to execute after the shot is complete

-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (nsdictionary *) info

{

Get Pictures

UIImage * image = [info objectforkey:uiimagepickercontrolleroriginalimage];

Picture in album

Uiimagewritetosavedphotosalbum (image, nil, nil, nil);

[Self dismissmodalviewcontrolleranimated:yes];

}

Execute method after clicking Cancel button

-(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) picker

{

[Self dismissmodalviewcontrolleranimated:yes];

}

Call camera photo and save to Picture library has been completed.

Next, open photo Gallery:

The code is as follows:

-(void) openpiclibrary

{

Albums can be opened with an emulator

if ([Uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypephotolibrary]) {

Uiimagepickercontroller * Picker = [[Uiimagepickercontroller alloc]init];

Picker.delegate = self;

Picker.allowsediting = yes;//Whether you can edit

Open album Select photos

Picker.sourcetype = uiimagepickercontrollersourcetypephotolibrary;

[Self presentmodalviewcontroller:picker animated:yes];

[Picker release];

}else{

Uialertview *alert = [[Uialertview alloc] initwithtitle:@ "Error" message:@ "You don't have a webcam delegate:nil cancelbuttontitle:@" Drat! "Otherbuttontitles:nil];

[Alert show];

}

}

Select the Proxy method to enter the picture

-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker didfinishpickingimage: (uiimage *) Image Editinginfo :(nsdictionary *) editinginfo

{

[Self dismissmodalviewcontrolleranimated:yes];

}

Call the flash code, because I am not very understanding, so I can not annotate, but has been pro-test available, but the Flash is a bug bar, the flash will be idle, and then continue to light

The code is as follows:

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

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 session

[Session startrunning];

Keep the session Around

[Self setavsession:self. Avsession];

[Output RELEASE];

}

}

-(void) closeflashlight

{

[Self. Avsession stoprunning];

[Self. Avsession release];

}

The above mentioned is the entire content of this article, I hope you can enjoy.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.