相機相冊閃關燈

#import "RootViewController.h"

#import#define KScreenWidth [[UIScreen mainScreen]bounds].size.width

#define KScreenHeight [[UIScreen mainScreen]bounds].size.height

@interface RootViewController ()

@property(nonatomic, strong)UIImageView *imageView;// 需要引入 #import// 獲取硬件設備

@property(nonatomic, strong)AVCaptureDevice *device;

@end

@implementation RootViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

self.navigationController.navigationBar.translucent = NO;

self.navigationItem.title = @"相機相冊閃光燈";

self.imageView = [[UIImageView alloc]initWithFrame:CGRectMake((KScreenWidth - 100) / 2, 30, 100, 100)];

self.imageView.backgroundColor = [UIColor cyanColor];

[self.view addSubview:_imageView];

UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];

[button setTitle:@"選擇" forState:UIControlStateNormal];

button.frame = CGRectMake((KScreenWidth - 100) / 2, 150, 100, 30);

[button addTarget:self action:@selector(actionSheetAction:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:button];

UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeSystem];

[closeButton setTitle:@"關閉閃關燈" forState:UIControlStateNormal];

closeButton.frame = CGRectMake((KScreenWidth - 100) / 2, 200, 100, 30);

[closeButton addTarget:self action:@selector(closeButton:) forControlEvents:UIControlEventTouchUpInside];

[self.view addSubview:closeButton];

}

- (void)closeButton:(UIButton *)button

{

// 如果閃光燈已經打開,那么把閃光燈關閉

if (self.device.torchMode == AVCaptureTorchModeOn)

{

[self.device setTorchMode:AVCaptureTorchModeOff];

[self.device unlockForConfiguration]; // 解除對設備硬件的獨占

}

// 如果閃光燈是關閉狀態則提示已經是關閉

else

{

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"閃關燈是關閉狀態" message:nil delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];

[alertView show];

}

}

#pragma mark -? 點擊頭像button觸發方法 彈出actionSheet -

- (void)actionSheetAction:(UIButton *)button

{

UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"相冊",@"閃光燈", nil];

actionSheet.actionSheetStyle = UIActionSheetStyleDefault;

[actionSheet showInView:self.view];

}

#pragma mark ----- ActionSheet觸發方法 -----

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

// 調用系統相機

if (buttonIndex == 0)

{

// 如果有系統相機

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

{

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

picker.delegate = self;

picker.allowsEditing = YES;

//攝像頭

picker.sourceType = UIImagePickerControllerSourceTypeCamera;

[self presentViewController:picker animated:YES completion:nil];

}

//如果沒有系統相機提示用戶

else

{

UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"您的設備沒有攝像頭" message:nil delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];

[alertView show];

}

}

// 調用系統相冊

else if (buttonIndex == 1)

{

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {

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

picker.delegate = self;

picker.allowsEditing = YES;//是否可以編輯

//打開相冊選擇照片

picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

// 模態進入相冊

[self presentViewController:picker animated:YES completion:nil];

}

}

else if (buttonIndex == 2)

{

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])

{

self.device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo]; // 返回用于捕獲視頻數據的設備(攝像頭)

if (![self.device hasTorch]) {

NSLog(@"沒有閃光燈");

}else{

[self.device lockForConfiguration:nil]; // 請求獨占設備的硬件性能

if (self.device.torchMode == AVCaptureTorchModeOff) {

[self.device setTorchMode: AVCaptureTorchModeOn]; // 打開閃光燈

}

}

}

else

{

//如果當前設備沒有攝像頭

UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"哎呀,當前設備沒有攝像頭。" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil];

[alertView show];

}

}

}

#pragma mark - 拍攝完成后或者選擇相冊完成后自動調用的方法 -

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

// 存入系統相冊

// UIImageWriteToSavedPhotosAlbum(backImageView.image, nil, nil, nil);

//得到圖片

self.imageView.image = [info objectForKey:UIImagePickerControllerOriginalImage];

NSString *filePath = [self libirayFilePath];

NSLog(@"filePath === %@", filePath);

// 模態返回

[self dismissViewControllerAnimated:YES completion:nil];

}

- (NSString *)libirayFilePath

{

return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)firstObject];

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容