系統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
複製代碼
相關文章
相關標籤/搜索