How IOS calls system cameras and albums

Source: Internet
Author: User

#import "CameraViewController.h"

@interface Cameraviewcontroller ()

@end

@implementation Cameraviewcontroller

– (void) Viewdidload {

[Super Viewdidload];

Nsarray *arr = @[@ "Call camera", @ "Call Picture library", @ "Turn off lights", @ "Turn off flash"];

for (int i = 0; i < 4; i++) {

UIButton *button = [UIButton buttonwithtype:uibuttontypecustom];

Button.frame = CGRectMake (+) * i + 50, 100, 100);

[Button Settitle:arr[i] forstate: (UIControlStateNormal)];

[Button Settitlecolor:[uicolor Blackcolor] forstate: (UIControlStateNormal)];

if (i = = 0) {

[Button addtarget:self action: @selector (Addcarema) forControlEvents: (UIControlEventTouchUpInside)];

}else if (i = = 1)

{

[Button addtarget:self action: @selector (Openpiclibrary) forControlEvents: (UIControlEventTouchUpInside)];

}else if (i = = 2)

{

[Button addtarget:self action: @selector (Openflashlight) forControlEvents: (UIControlEventTouchUpInside)];

}

else if (i = = 3)

{

[Button addtarget:self action: @selector (Closeflashlight) forControlEvents: (UIControlEventTouchUpInside)];

}

[Self.view Addsubview:button];

}

Do no additional setup after loading the view.

}

-(void) Addcarema

{

The emulator cannot use this feature to determine if the camera can be turned on

if ([Uiimagepickercontroller issourcetypeavailable: (Uiimagepickercontrollersourcetypecamera)]) {

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

Picker.delegate = self;

picker.allowsediting = YES; Whether to edit

Camera

Picker.sourcetype = Uiimagepickercontrollersourcetypecamera;

[Self Presentviewcontroller:picker animated:yes completion:nil];

}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, nil];

[Alert show];

}

}

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 dismissviewcontrolleranimated:yes completion:nil];

}

Execute method after clicking Cancel button

-(void) Imagepickercontrollerdidcancel: (Uiimagepickercontroller *) picker

{

[Self dismissviewcontrolleranimated:yes completion:nil];

}

-(void) openpiclibrary

{

Albums can be opened with an emulator

if ([Uiimagepickercontroller issourcetypeavailable:uiimagepickercontrollersourcetypephotolibrary]) {

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

Picker.delegate = self;

picker.allowsediting = YES;

Open album Select photos

Picker.sourcetype = uiimagepickercontrollersourcetypephotolibrary;

[Self Presentviewcontroller:picker animated:yes completion:nil];

}else

{

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

[Alert show];

}

}

-(void) openflashlight

{

Avcapturedevice *device = [Avcapturedevice defaultdevicewithmediatype:avmediatypevideo];

if ([device Hastorch]) {

[Device Lockforconfiguration:nil];

[Device Settorchmode:avcapturetorchmodeoff];

[Device unlockforconfiguration];

}

}

-(void) closeflashlight

{

[Self. Avsession stoprunning];

}

– (void) Didreceivememorywarning {

[Super didreceivememorywarning];

Dispose of any of the can is recreated.

}

/*

#pragma mark–navigation

In a storyboard-based application, your'll 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

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.