YPImagePicker
YPImagePicker 是一个使用纯 Swift 编写的类似 Instagram 的照片/视频选择器,它功能丰富且高度可定制,以满足应用程序的需求。
试试它:先运行 pod repo update
,然后运行 pod try YPImagePicker
这些功能只需几行代码即可实现!
显著特性
还有很多...
安装
CocoaPods
使用首先,确保运行 pod repo update
以获取最新版本。
在您的 Podfile
中添加 pod 'YPImagePicker'
并运行 pod install
。同时请将 use_frameworks!
添加到 Podfile
中。
target 'MyApp'
pod 'YPImagePicker'
use_frameworks!
Swift Package Manager
使用通过 文件 > Swift Pakcages > 添加包依赖...
打开 SPM 依赖管理器。
并插入仓库 URL
https://github.com/Yummypets/YPImagePicker.git
要在您的包中添加依赖,只需在 Package.swift
的依赖中指定一个包
.package(
name: "YPImagePicker",
url: "https://github.com/Yummypets/YPImagePicker.git",
.upToNextMajor(from: "5.0.0")
)
注意:此库至少需要 iOS 版本 12.0
。
Plist条目
为了让您的应用访问相机和相册,您需要添加以下 plist条目
- 隐私 - 摄像头使用描述(照片/视频)
- 隐私 - 照片库使用描述(库)
- 隐私 - 麦克风使用描述(视频)
<key>NSCameraUsageDescription</key>
<string>yourWording</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>yourWording</string>
<key>NSMicrophoneUsageDescription</key>
<string>yourWording</string>
配置
所有配置端点都在 YPImagePickerConfiguration 结构中。以下是一些默认值以便参考,您可以自由尝试 :)
var config = YPImagePickerConfiguration()
// [Edit configuration here ...]
// Build a picker with your configuration
let picker = YPImagePicker(configuration: config)
常规
config.isScrollToChangeModesEnabled = true
config.onlySquareImagesFromCamera = true
config.usesFrontCamera = false
config.showsPhotoFilters = true
config.showsVideoTrimmer = true
config.shouldSaveNewPicturesToAlbum = true
config.albumName = "DefaultYPImagePickerAlbumName"
config.startOnScreen = YPPickerScreen.photo
config.screens = [.library, .photo]
config.showsCrop = .none
config.targetImageSize = YPImageSize.original
config.overlayView = UIView()
config.hidesStatusBar = true
config.hidesBottomBar = false
config.hidesCancelButton = false
config.preferredStatusBarStyle = UIStatusBarStyle.default
config.bottomMenuItemSelectedColour = UIColor(r: 38, g: 38, b: 38)
config.bottomMenuItemUnSelectedColour = UIColor(r: 153, g: 153, b: 153)
config.filters = [DefaultYPFilters...]
config.maxCameraZoomFactor = 1.0
config.preSelectItemOnMultipleSelection = true
config.fonts..
图书馆
config.library.options = nil
config.library.onlySquare = false
config.library.isSquareByDefault = true
config.library.minWidthForItem = nil
config.library.mediaType = YPlibraryMediaType.photo
config.library.defaultMultipleSelection = false
config.library.maxNumberOfItems = 1
config.library.minNumberOfItems = 1
config.library.numberOfItemsInRow = 4
config.library.spacingBetweenItems = 1.0
config.library.skipSelectionsGallery = false
config.library.preselectedItems = nil
视频
config.video.compression = AVAssetExportPresetHighestQuality
config.video.fileType = .mov
config.video.recordingTimeLimit = 60.0
config.video.libraryTimeLimit = 60.0
config.video.minimumTimeLimit = 3.0
config.video.trimmerMaxDuration = 60.0
config.video.trimmerMinDuration = 3.0
相册
config.gallery.hidesRemoveButton = false
默认配置
// Set the default configuration for all pickers
YPImagePickerConfiguration.shared = config
// And then use the default configuration like so:
let picker = YPImagePicker()
在iPad上显示选择器时,选择器只支持一种大小,您应在其显示之前设置。
let preferredContentSize = CGSize(width: 500, height: 600);
YPImagePickerConfiguration.widthOniPad = preferredContentSize.width;
// Now you can Display the picker with preferred size in dialog, popup etc
使用方法
首先,导入 YPImagePicker
。
选择器只有一个回调 didFinishPicking
,使您可以处理所有情况。让我们看看一些典型用法
单张图片
let picker = YPImagePicker()
picker.didFinishPicking { [unowned picker] items, _ in
if let photo = items.singlePhoto {
print(photo.fromCamera) // Image source (camera or library)
print(photo.image) // Final image selected by the user
print(photo.originalImage) // original image selected by the user, unfiltered
print(photo.modifiedImage) // Transformed image, can be nil
print(photo.exifMeta) // Print exif meta data of original image.
}
picker.dismiss(animated: true, completion: nil)
}
present(picker, animated: true, completion: nil)
单视频
// Here we configure the picker to only show videos, no photos.
var config = YPImagePickerConfiguration()
config.screens = [.library, .video]
config.library.mediaType = .video
let picker = YPImagePicker(configuration: config)
picker.didFinishPicking { [unowned picker] items, _ in
if let video = items.singleVideo {
print(video.fromCamera)
print(video.thumbnail)
print(video.url)
}
picker.dismiss(animated: true, completion: nil)
}
present(picker, animated: true, completion: nil)
正如您所看到的,singlePhoto
和 singleVideo
辅助工具都在这里,帮助您处理单个媒体,这些媒体非常常见,同时使用相同的回调处理所有场景 \o/
多选
要启用多选,请确保在配置中将 library.maxNumberOfItems
设置如下
var config = YPImagePickerConfiguration()
config.library.maxNumberOfItems = 3
let picker = YPImagePicker(configuration: config)
然后您可以在您熟悉和喜爱的同一个回调函数中处理多选
picker.didFinishPicking { [unowned picker] items, cancelled in
for item in items {
switch item {
case .photo(let photo):
print(photo)
case .video(let video):
print(video)
}
}
picker.dismiss(animated: true, completion: nil)
}
处理取消事件(如果需要的话)
picker.didFinishPicking { [unowned picker] items, cancelled in
if cancelled {
print("Picker was canceled")
}
picker.dismiss(animated: true, completion: nil)
}
这样就可以了!
语言
如果您的语言不支持,您仍然可以通过 configuration.wordings
api 来自定义文本
config.wordings.libraryTitle = "Gallery"
config.wordings.cameraTitle = "Camera"
config.wordings.next = "OK"
更好的是,您可以提交一个issue或pull request,附带您的 Localizable.strings
文件来添加新语言!
UI 定制化
我们尽量做到本地化,因此主要通过本地API完成。
导航栏颜色
let coloredImage = UIImage(color: .red)
UINavigationBar.appearance().setBackgroundImage(coloredImage, for: UIBarMetrics.default)
// UIImage+color helper https://stackoverflow.com/questions/26542035/create-uiimage-with-solid-color-in-swift
导航栏字体
let attributes = [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 30, weight: .bold) ]
UINavigationBar.appearance().titleTextAttributes = attributes // Title fonts
UIBarButtonItem.appearance().setTitleTextAttributes(attributes, for: .normal) // Bar Button fonts
导航栏文本颜色
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor : UIColor.yellow ] // Title color
UINavigationBar.appearance().tintColor = .red // Left. bar buttons
config.colors.tintColor = .green // Right bar buttons (actions)
原始项目 & 作者
该项目最初受到 Fusuma 的启发。考虑到代码量大、设计变更以及一路上添加的所有附加功能,该项目从分叉转变为独立的单独存储库,也为了便于发现。原始 Fusuma 作者为 ytakz
核心团队


🙏
贡献者ezisazis,hanikeddah,tahaburak,ajkolean,Anarchoschnitzel,Emil,Rafael Damasceno,cenkingunlugu heitara portellaa Romixery shotat shalamowww
特别感谢 ihtiht 为我们设计了酷炫的标志!
👏
他们在不同的方式上帮助我们userdar,Evgeniy,MehdiMahdloo,om-ha,userdar,ChintanWeapp,eddieespinal,viktorgardart,gdelarosa,cwestMobile,Tinyik,Vivekthakur647,tomasbykowski,artemsmikh,theolof,dongdong3344,MHX792,CIronfounderson,Guerrix,Zedd0202,mohammadZ74,SalmanGhumsani,wegweiser6,BilalAkram,KazimAhmad,JustinBeBoy,SashaMeyer,GShushanik,Cez95,Palando,sebastienboulogne,JigneshParekh7165,Deepakepaisa,AndreiBoariu,nathankonrad1,wawilliams003,pngo-hypewell,PawanManjani,devender54321,Didar1994,relaxsus restoflash
依赖关系
YPImagePicker 需要依赖 prynt/PryntTrimmerView 来提供视频剪辑和封面功能。非常感谢 @HHK1 将此开源 :)
Obj-C 支持
Objective-C 不受支持,这并不是我们的开发路线图。Swift 是未来,为了让这个库保持速度,我们需要放弃 Obj-C :)
许可证
YPImagePicker 是在 MIT 许可下发布的。
有关详细信息,请参阅 LICENSE。