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

电脑杂谈  发布时间:2020-06-29 09:08:38  来源:网络整理

ios调用系统相机相册_android调用系统相机_ios调用系统相机相册

今天与您分享如何调用iPhone的相机功能并打开闪光灯. 我对某些代码不太了解,其中许多代码是从网上其他代码中借来的. IOS有两种拍摄照片和视频的方法: 1.直接使用UIImagePickerController,此类提供了一种简单便捷的功能,可以在图片库中拍摄图片和选择图片. 2.另一个是完全自定义拍照界面,并通过AVFoundation.framework框架选择图片库的界面. 我只制作了第一个,因此我将首先向您介绍:

首先,在首先调用该接口之前,我们需要确定当前设备是否支持UIImagePickerController,使用isSourceTypeAvailable: 确定其是否可用

第二,检查兼容的媒体类型,这一次我们称为availableMediaTypeForSourceType: 判断

ios调用系统相机相册_ios调用系统相机相册_android调用系统相机

调用UIImagePickerController时,我们需要添加他的两个代理方法:

UINavigationControllerDelegate和UIImagePickerControllerDelegate,您也可以在调用相机时调整闪光灯ios调用系统相机相册,代码中会出现一段时间.

要调用Flash,您需要创建AVCaptureSession类的实例对象:

android调用系统相机_ios调用系统相机相册_ios调用系统相机相册

//
//  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调用图像库,手电筒打开闪光灯,关闭关闭闪光灯,我不会在此处编写用于创建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];
    }
}

ios调用系统相机相册_ios调用系统相机相册_android调用系统相机

打开相机后,您需要在UIImagePickerControllerDelegate中调用该方法,在完成拍摄后执行的方法以及在单击“取消”后执行的方法:

//拍摄完成后要执行的方法
-(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];
}

ios调用系统相机相册_ios调用系统相机相册_android调用系统相机

调用照相机照片并将其保存到图片库已完成.

然后介绍照片库的打开:

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

因为我不太了解调用闪光灯的代码,所以无法添加注释,但是该注释已经可以用于个人测试,但是在调整闪光灯时会出现错误. 闪光灯将闲置一会儿ios调用系统相机相册,然后继续亮起


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/ruanjian/article-262193-1.html

    相关阅读
      发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

      • 贾超杰
        贾超杰

        给你点个赞

      • 瑚冬梅
        瑚冬梅

        当然如果你还给孩子还房贷什么的就没办法了

      热点图片
      拼命载入中...