IOS开发调用系统相机和打开闪光灯

本博客介绍如何在应用中集成相机与相册选择功能,实现用户拍照或从相册选择图片,并保存到本地。同时,展示了如何通过代码调用闪光灯功能,提升用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#import "RootViewController.h"

@interface RootViewController ()<UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>
{
    
}

@property (weak, nonatomic) IBOutlet UIButton *selectButton;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation RootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
}
- (IBAction)selectionAction:(id)sender {
    NSLog(@"选择。。。");
    
    UIActionSheet *actionSheet = [[UIActionSheet alloc]initWithTitle:@"请选择" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相机",@"相册选择", nil];
    [actionSheet showInView:sender];
    
}

#pragma mark -- UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        NSLog(@"相机...");
        
        //判断是否可以打开相机,模拟器此功能无法使用
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
            
            UIImagePickerController * picker = [[UIImagePickerController alloc]init];
            picker.delegate = self;
            picker.allowsEditing = YES;  //是否可编辑
            //摄像头
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
            [self presentModalViewController:picker animated:YES];
            
        }else{
            //如果没有提示用户
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:nil];
            [alert show];
        }
        
    } else if (buttonIndex == 1) {
        NSLog(@"相册");
        //相册是可以用模拟器打开的
        if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
            UIImagePickerController * picker = [[UIImagePickerController alloc]init];
            picker.delegate = self;
            picker.allowsEditing = YES;//是否可以编辑
            
            //打开相册选择照片
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentModalViewController:picker  animated:YES];
            
        }else{
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"你没有摄像头" delegate:nil cancelButtonTitle:@"Drat!" otherButtonTitles:nil];
            [alert show];
        }
    }
}


#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    [picker dismissViewControllerAnimated:YES completion:^{
        
    }];
    
    
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    
    
    // 保存图片至本地
    [self saveImage:image withName:@"currentImage.png"];
    
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
    
    UIImage *saveImage = [[UIImage alloc]initWithContentsOfFile:fullPath];
    
    [self.imageView setImage:saveImage];
    
    self.imageView.tag = 100;
    
    
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


#pragma mark - 保存图片至沙盒
- (void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
    //高保真压缩图片方法
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    
    // 获取沙盒目录
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    
    //将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
}


@@@@@@@@@@@@@@@@@@@@@@@@@@@@


调用闪光灯的代码,由于我也不是很理解,所以没法加注释,但是已经亲测可用,但是调闪光灯时有一个算是bug吧,闪光灯会闲一下,然后再一直亮
-(void)openFlashlight
{
 

  AVCaptureDevice * device = [AVCaptureDevicedefaultDeviceWithMediaType:AVMediaTypeVideo];
    if(device.torchMode == AVCaptureTorchModeOff) {
       //Create an AV session
       AVCaptureSession * session = [[AVCaptureSession alloc]init];
        
       // Create device input and add to current session
       AVCaptureDeviceInput * input = [AVCaptureDeviceInputdeviceInputWithDevice:device error:nil];
       [session addInput:input];
        
       // Create video output and add to current session
       AVCaptureVideoDataOutput * output = [[AVCaptureVideoDataOutputalloc]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];
        
    
    }
}
 
-(void)closeFlashlight
{
   [self.AVSession stopRunning];
  
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值