一款图片/视频选择器-支持LivePhoto、GIF选择、iCloud/网络资源在线下载、图片/视频编辑
- iOS 12.0+
- Xcode 12.5+
- Swift 5.4+
⚠️ 需要 Xcode 12.0 及以上版本来支持资源文件/本地化文件的添加。
dependencies: [
.package(url: "https://github.com/SilenceLove/HXPhotoPicker.git", .upToNextMajor(from: "4.0.2"))
]
将下面内容添加到 Podfile
,并执行依赖更新。
iOS 12.0+
pod 'HXPhotoPicker'
/// 不包含 Kingfisher,相机不包含定位功能
pod `HXPhotoPicker/Lite`
/// 相机不包含定位功能
pod `HXPhotoPicker/NoLocation`
/// 只有选择器
pod `HXPhotoPicker/Picker`
pod `HXPhotoPicker/Picker/Lite`
/// 只有编辑器
pod `HXPhotoPicker/Editor`
pod `HXPhotoPicker/Editor/Lite`
/// 只有相机
pod `HXPhotoPicker/Camera`
/// 不包含定位功能
pod `HXPhotoPicker/Camera/Lite`
iOS 10.0+
pod 'HXPhotoPicker-Lite'
pod 'HXPhotoPicker-Lite/Picker'
pod 'HXPhotoPicker-Lite/Editor'
pod 'HXPhotoPicker-Lite/Camera'
v4.0以下的ObjC版本
pod 'HXPhotoPickerObjC'
按需在你的 Info.plist 中添加以下键值:
Key |
模块 |
备注 |
NSPhotoLibraryUsageDescription |
Picker |
允许访问相册 |
NSPhotoLibraryAddUsageDescription |
Picker |
允许保存图片至相册 |
PHPhotoLibraryPreventAutomaticLimitedAccessAlert |
Picker |
设置为 YES iOS 14+ 以禁用自动弹出添加更多照片的弹框(Picker 已适配 Limited 功能,可由用户主动触发,提升用户体验) |
NSCameraUsageDescription |
Camera |
允许使用相机 |
NSMicrophoneUsageDescription |
Camera |
允许使用麦克风 |
import HXPhotoPicker
class ViewController: UIViewController {
func presentPickerController() {
// 设置与微信主题一致的配置
let config = PickerConfiguration.default
// 方法一:async/await
let images: [UIImage] = try await Photo.picker(config)
let urls: [URL] = try await Photo.picker(config)
let results: [AssetURLResult] = try await Photo.picker(config)
let pickerResult = try await Photo.picker(config)
let images: [UIImage] = try await pickerResult.objects()
let urls: [URL] = try await pickerResult.objects()
let urlResults: [AssetURLResult] = try await pickerResult.objects()
// 方法二:
let pickerController = PhotoPickerController(picker: config)
pickerController.pickerDelegate = self
// 当前被选择的资源对应的 PhotoAsset 对象数组
pickerController.selectedAssetArray = selectedAssets
// 是否选中原图
pickerController.isOriginal = isOriginal
present(pickerController, animated: true, completion: nil)
// 方法三:
Photo.picker(
config
) { result, pickerController in
// 选择完成的回调
// result 选择结果
// .photoAssets 当前选择的数据
// .isOriginal 是否选中了原图
// photoPickerController 对应的照片选择控制器
} cancel: { pickerController in
// 取消的回调
// photoPickerController 对应的照片选择控制器
}
}
}
extension ViewController: PhotoPickerControllerDelegate {
/// 选择完成之后调用
/// - Parameters:
/// - pickerController: 对应的 PhotoPickerController
/// - result: 选择的结果
/// result.photoAssets 选择的资源数组
/// result.isOriginal 是否选中原图
func pickerController(
_ pickerController: PhotoPickerController,
didFinishSelection result: PickerResult
) {
// async/await
let images: [UIImage] = try await result.objects()
let urls: [URL] = try await result.objects()
let urlResults: [AssetURLResult] = try await result.objects()
result.getImage { (image, photoAsset, index) in
if let image = image {
print("success", image)
}else {
print("failed")
}
} completionHandler: { (images) in
print(images)
}
}
/// 点击取消时调用
/// - Parameter pickerController: 对应的 PhotoPickerController
func pickerController(didCancel pickerController: PhotoPickerController) {
}
}
/// 如果为视频的话获取则是视频封面
// async/await
// compression: 压缩参数,不传则不压缩
let image: UIImage = try await photoAsset.object(compression)
/// compressionQuality: 压缩参数,不传则不压缩
photoAsset.getImage(compressionQuality: compressionQuality) { image in
print(image)
}
// async/await
// compression: 压缩参数,不传则不压缩
let url: URL = try await photoAsset.object(compression)
let result: AssetURLResult = try await photoAsset.object(compression)
/// compression: 压缩参数,不传则不压缩
photoAsset.getURL(compression: compression) { result in
switch result {
case .success(let urlResult):
// 媒体类型
switch urlResult.mediaType {
case .photo:
// 图片
case .video:
// 视频
}
// url类型
switch urlResult.urlType {
case .local:
// 本地URL
case .network:
// 网络URL
}
// 获取的地址
print(urlResult.url)
// LivePhoto 里面包含的 图片和视频 url
print(urlResult.livePhoto)
case .failure(let error):
print(error)
}
}
版本 |
发布时间 |
Xcode |
Swift |
iOS |
v4.0.2 |
2023-06-24 |
14.3.0 |
5.7.0 |
12.0+ |
v4.0.1 |
2023-06-17 |
14.3.0 |
5.7.0 |
12.0+ |
v4.0.0 |
2023-06-15 |
14.3.0 |
5.7.0 |
12.0+ |
v3.0.0 |
2022-09-18 |
14.0.0 |
----- |
8.0+ |
HXPhotoPicker 基于 MIT 协议进行分发和使用,更多信息参见协议文件。
![Stargazers over time](https://camo.githubusercontent.com/ab000977db2c91976cd4341e8b4d30130a9ee2284557a8d1d7b199537b0224a4/68747470733a2f2f7374617263686172742e63632f53696c656e63654c6f76652f485850686f746f5069636b65722e737667)
🔝回到顶部