系统SDK介绍-02

系统SDK介绍

  1. 打开相册选择图片
  2. 打开相册选择视频
  3. 打开相机拍摄图片
  4. 打开相机拍摄视频

配置权限:

在info.plist文件中添加需要的权限
  • 相机权限:Privacy - Camera Usage Description 允许此权限才能使用相机功,这样才能录制视频,并且想要保存图片。
  • 相册权限:Privacy - Photo Library Usage Description 允许此权限才能使用系统相册。
  • 麦克风权限:Privacy - Microphone Usage Description 获取麦克风权限不然会崩,只有允许此权限才能录音。

判断是否权限

#pragma mark - 权限判断
- (BOOL)authorizationCamera {
    NSString *mediaType = AVMediaTypeVideo;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
        return false;
    }
    return true;
}
复制代码

配置选项

typedef NS_ENUM(NSInteger, LZSystemPhotoSelectorType) {
    LZSystemPhotoSelectorTypeVideo, // 选择视频
    LZSystemPhotoSelectorTypePhoto, // 选择图片
};

typedef NS_ENUM(NSInteger, LZSystemOpenDeviceType) {
    LZSystemOpenDeviceTypeCamera, // 打开相机
    LZSystemOpenDeviceTypeVideo,  // 打开摄像机
};
复制代码

接口文件

#import <Foundation/Foundation.h>

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(NSInteger, LZSystemPhotoSelectorType) {
    LZSystemPhotoSelectorTypeVideo, // 选择视频
    LZSystemPhotoSelectorTypePhoto, // 选择图片
};


typedef NS_ENUM(NSInteger, LZSystemOpenDeviceType) {
    LZSystemOpenDeviceTypeCamera, // 打开相机
    LZSystemOpenDeviceTypeVideo,  // 打开摄像机
};

@interface LZSystemPhotoSelector : NSObject

+ (instancetype)selector;

/**
 选择图片或视频

 @param type 类型
 @param allowsEditing 是否允许编辑
 @param resultFile 选择结果,图片是UIImage 视频是NSUrl
 */
- (void)lz_openAblumWithType:(LZSystemPhotoSelectorType)type allowsEditing:(BOOL)allowsEditing resultFile:(void(^)(id info))resultFile;


/**
 打开相机或摄像机

 @param type 类型
 @param allowsEditing 是否拍摄完成进行编辑
 @param resultFile 选择结果,图片是UIImage 视频是NSUrl
 */
- (void)lz_openDeviceWithType:(LZSystemOpenDeviceType)type allowsEditing:(BOOL)allowsEditing resultFile:(void(^)(id info))resultFile;

@end

NS_ASSUME_NONNULL_END
复制代码

实现文件

#import "LZSystemPhotoSelector.h"
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AssetsLibrary/AssetsLibrary.h>
#import <MobileCoreServices/MobileCoreServices.h>

#define kRootViewController UIApplication.sharedApplication.delegate.window.rootViewController

@interface LZSystemPhotoSelector () <UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (nonatomic, copy) void (^resultFile)(id info);
@property (nonatomic, assign) BOOL allowsEditing;

@end

@implementation LZSystemPhotoSelector

+ (instancetype)selector {
    static dispatch_once_t onceToken;
    static LZSystemPhotoSelector *selector;
    dispatch_once(&onceToken, ^{
        selector = [[LZSystemPhotoSelector alloc] init];
    });
    return selector;
}

#pragma mark - 选择图片或视频
- (void)lz_openAblumWithType:(LZSystemPhotoSelectorType)type allowsEditing:(BOOL)allowsEditing resultFile:(void(^)(id info))resultFile {
    
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        resultFile(@"该设备暂时不支持");
        return;
    }

    self.allowsEditing = allowsEditing;
    self.resultFile = resultFile;
    
    UIImagePickerController *controller = [[UIImagePickerController alloc] init];
    controller.delegate = self;
    controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    if (type == LZSystemPhotoSelectorTypePhoto) {
        controller.mediaTypes = [NSArray arrayWithObjects:@"public.image", nil];
    } else {
        controller.mediaTypes = [NSArray arrayWithObjects:@"public.movie", nil];
    }
    controller.allowsEditing = allowsEditing;
    [kRootViewController presentViewController:controller animated:true completion:nil];
}

#pragma mark - 打开相机或摄像机
- (void)lz_openDeviceWithType:(LZSystemOpenDeviceType)type allowsEditing:(BOOL)allowsEditing resultFile:(void (^)(id _Nonnull))resultFile {
    
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        resultFile(@"该设备暂时不支持");
        return;
    }
    
    if (![self authorizationCamera]) {
        resultFile(@"暂无相机权限");
        return;
    }
    
    self.allowsEditing = allowsEditing;
    self.resultFile = resultFile;
    
    UIImagePickerController *controller = [[UIImagePickerController alloc] init];
    controller.delegate = self;
    controller.sourceType = UIImagePickerControllerSourceTypeCamera;

    if (type == LZSystemOpenDeviceTypeCamera) {
        controller.mediaTypes = [NSArray arrayWithObjects:@"public.image", nil];
    } else {
        controller.mediaTypes = [NSArray arrayWithObjects:@"public.movie", nil];
    }
    controller.allowsEditing = allowsEditing;
    [kRootViewController presentViewController:controller animated:true completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info {

    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];

    if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {
        if (self.allowsEditing) {
            UIImage *editorImage = info[UIImagePickerControllerEditedImage];
            self.resultFile(editorImage);
        } else {
            UIImage *editorImage = info[UIImagePickerControllerOriginalImage];
            self.resultFile(editorImage);
        }
    } else {
        NSURL *url = info[UIImagePickerControllerMediaURL];
        self.resultFile(url);
    }
    
    [picker dismissViewControllerAnimated:true completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [picker dismissViewControllerAnimated:true completion:nil];
}

#pragma mark - 权限判断
- (BOOL)authorizationCamera {
    NSString *mediaType = AVMediaTypeVideo;
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
    if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
        return false;
    }
    return true;
}
@end
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值