ADPhotoKit 是一个纯 Swift 库,用于从系统相册中选择资源(例如:照片、视频、gif、livephoto)。默认外观类似于微信。
最简单的用例是在您的控制器上显示图像选择器
ADPhotoKitUI.imagePicker(present: self) { (assets, origin) in
// do something
}
您也可以在 SwiftUI 上显示图像选择器
import SwiftUI
struct SwiftUIView: View {
@State private var showImagePicker = false
var body: some View {
Button("PickerImage") {
showImagePicker.toggle()
}
.imagePicker(isPresented: $showImagePicker,
selected: { (assets, origin) in
// do something
})
}
}
选择最多 9 张图像或视频
ADPhotoKitUI.imagePicker(present: self,
params: [.maxCount(max: 9)],
selected: { (assets, origin) in
// do something
})
选择 1 个视频或 9 张图像
ADPhotoKitUI.imagePicker(present: self,
assetOpts: .exclusive,
params: [.maxCount(max: 9),.imageCount(min: nil, max: 9),.videoCount(min: nil, max: 1)],
selected: { (assets, origin) in
// do something
})
选择最多 8 张图像
ADPhotoKitUI.imagePicker(present: self,
albumOpts: [.allowImage],
assetOpts: .exclusive,
params: [.maxCount(max: 8)],
selected: { (assets, origin) in
// do something
})
浏览网络图像和视频
ADPhotoKitUI.assetBrowser(present: self,
assets: [NetImage(url: "https://example.com/xx.png"), NetVideo(url: "https://example.com/xx.mp4")]) { assets in
// do something
}
有关更多使用配置,您可以查看 ADPhotoKitConfiguration 和 SelectionRestrict。
要了解更多 ADPhotoKit 的使用方法,请参考示例和 API 参考。
不支持 Objective-C。Swift 是未来,放弃 Obj-C 是保持我们在这个库上开发速度的代价 :)
ADPhotoKit 可通过 CocoaPods 获得。要安装它,只需将以下行添加到您的 Podfile 中
source 'https://cdn.cocoapods.org/'
platform :ios, '10.0'
use_frameworks!
target 'MyApp' do
pod 'ADPhotoKit'
end
DocC 支持需要 cocoaPods 1.12.0+
现在有 4 个 subspecs 可用
Subspec | 描述 |
---|---|
Base | 必需。此 subspec 提供基本配置和扩展。 |
Core | 可选。此 subspec 提供原始数据。依赖于 Base subspec。 |
CoreUI | 可选。此 subspec 提供用于照片选择的 UI。依赖于 Core subspec。 |
ImageEdit | 可选。此 subspec 提供图像编辑功能。依赖于 Base subspec。 |
VideoEdit | 可选。此 subspec 提供视频编辑功能。依赖于 Base subspec。 |
您可以仅安装 ADPhotoKit 的某些模块。默认情况下,您将获得 CoreUI
subspecs。
您需要在应用程序的 Info.plist 中添加以下键值对
// If you don’t add this key-value pair, multiple languages are not supported, and the system PhotoKitUI language defaults to English
Localized resources can be mixed YES
// You must add follow in your app's Info.plist
Privacy - Photo Library Usage Description
// If you `assetOpts` contain `allowTakePhotoAsset`, you must add follow
Privacy - Camera Usage Description
// If you `assetOpts` contain `allowTakeVideoAsset`, you must add follow
Privacy - Microphone Usage Description
如果您有功能请求或错误报告,请随时通过发送 pull 请求或创建新 issue 来提供帮助。
ADPhotoKit 在 MIT 许可证下可用。有关更多信息,请参见 LICENSE 文件。
一些代码和资源复制自 ZLPhotoBrowser