AvatarImagePicker & AuthSettings
描述
AvatarImagePicker 是一个用于 iOS 的照片库和相机图像选择器
它使用 Swift 编写,只需一行代码即可支持通过相机或照片库选择用户头像,并编辑所选图像。它还支持身份验证验证,如果摄像头或照片库被拒绝,它将提醒用户打开它的设置。这意味着可替代 UIKit 的 UIImagePickerController。兼容 Swift 和 Objective-C。
安装
CocoaPods
pod 'AvatarImagePicker'
Carthage
github "janlionly/AvatarImagePicker"
Swift 包管理器
- iOS: 打开 Xcode,文件->Swift 包,搜索输入 https://github.com/janlionly/AvatarImagePicker.git,然后选择升级到下一个主要版本 1.3.0 < .
- 或者在您的
Package.swift
中添加依赖项
.package(url: "https://github.com/janlionly/AvatarImagePicker.git", .upToNextMajor(from: "1.3.0")),
使用方法
AvatarImagePicker 以操作表的形式显示相机和照片库,然后显示 ImagePickerController,可选地支持一些可定制的属性,例如 sourceTypes,presentStyle 和 dismissAnimated。
请记得将 NSCameraUsageDescription 和 NSPhotoLibraryUsageDescription 的键值添加到您的 Info.plist 以进行描述。
Swift
let picker = AvatarImagePicker.instance
// optional: custom properties
picker.dismissAnimated = false // default is true
picker.sourceTypes = [.camera] // default is [.camera, .photoLibrary]
picker.presentStyle = .overFullScreen // default is .fullScreen
// this method includes authorizing for photolibrary and camera.
picker.present(allowsEditing: true, selected: { (image) in
// selected image
}) {
// tapped cancel
}
// v1.3.2 updated: support to customize actions for sheet
picker.sourceTypes = [.camera, .customAction]
weak var weakSelf = self
picker.customActions = ["Delete": { weakSelf?.imageView.image = nil }]
// or you can call only auth photolibrary and camera, it will alert the user to go to settings if the photolibrary or camera was denied.
let isAuthSuccess = AuthSettings.authPhotoLibrary(message: "auth photolibrary to get your avatar") {
print("auth success")
// go to present image picker controller(photo library), customize your operation here.
}
_ = AuthSettings.authCamera(message: "auth camera to get your avatar") {
print("auth success")
// go to present image picker controller(camera), customize your operation here.
}
Objective-C
[[AvatarImagePicker avatarImagePicker] presentWithAllowsEditing:YES selected:^(UIImage * _Nonnull image) {
// selected image
} cancel:^{
// tapped cancel
}];
要求
- iOS 9.0+
- Swift 4.2 到 5.2
作者
访问我的 GitHub: janlionly
通过电子邮件与我联系: [email protected]
贡献
我很希望您能够为 AvatarImagePicker 做出贡献
许可证
AvatarImagePicker 采用 MIT 许可证。详细信息请参阅 LICENSE 文件。