SwiftyPhotos
用于增强生产力的 PhotoKit 框架 的有用工具。
要求
Xcode 10(或更高版本)和 Swift 5。此库是为 iOS 10.0 或更高版本制作的。
安装
CocoaPods
CocoaPods 是 Cocoa 项目的依赖管理器。您可以使用以下命令安装它
$ gem install cocoapods
要使用 CocoaPods 将 SwiftyPhotos 集成到您的 Xcode 项目中,请在您的 Podfile
中指定它
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '10.0'
use_frameworks!
target '<Your Target Name>' do
pod 'SwiftyPhotos'
end
然后运行以下命令
$ pod install
手动
将 源 文件夹添加到您的 Xcode 项目中,以使用所有扩展,或特定的扩展。
使用说明
授权状态
SwiftyPhotos.shared.reloadAll { (isPhotoAuthrized) in
if isPhotoAuthrized {
if let allPhotosAlbum = SwiftyPhotos.shared.allPhotoAlbums.first {
self.photoAlbum = allPhotosAlbum
}
DispatchQueue.main.async {
self.setupUI()
}
} else {
print("please allow photo authorization status")
DispatchQueue.main.async {
let alertVC = UIAlertController(title: "Fail to visit iPhone photo album", message: nil, preferredStyle: .alert)
let goSettings = UIAlertAction(title: "Go to Settings", style: .default, handler: { (alertAction) in
print("go to settings")
if let url = URL(string: UIApplicationOpenSettingsURLString) {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
})
let cancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
alertVC.addAction(goSettings)
alertVC.addAction(cancel)
self.present(alertVC, animated: true, completion: nil)
}
}
}
相册
PhotoAlbumModel 包含相册所需的所有内容。
检查相册是否存在或根据名称创建相册。
if let _ = SwiftyPhotos.shared.photoAlbumWithName("SwiftyPhotos") {
} else {
_ = SwiftyPhotos.shared.createAlbum("SwiftyPhotos")
}
创建一个 PhotoAssetsView,并使用 PhotoAlbumsViewDelegate 来处理照片选择动作。
let view = PhotoAssetsView(frame: frame, photoAlbum: self.photoAlbum, isKeepingPhotoRatio: false, cellCountOfLine: 3, cellOffset: 2.0)
public protocol PhotoAlbumsViewDelegate: class {
func PhotoAlbumsViewDidSelectPhotoAlbum(_ photoAlbum: PhotoAlbumModel)
}
照片
PhotoAssetModel 包含照片资产所需的所有内容。
请求照片的缩略图。
public var photoAsset: PhotoAssetModel! {
didSet {
self.imageRequestID = self.photoAsset.requestThumbnail(resultHandler: { (image, info) in
DispatchQueue.main.async {
if let info = info {
if let requestID = info[PHImageResultRequestIDKey] as? NSNumber {
if requestID.int32Value == self.imageRequestID {
self.thumbnail.image = image
}
}
}
}
})
}
}
从iCloud请求照片。
if self.photoAsset.isInCloud {
print("photo in icloud")
self.photoAsset.requestAvailableSizeImageInCloud { [weak self] (image, info) in
if let image = image {
self?.imageView.image = image
}
}
self.photoAsset.requestMaxSizeImageInCloud(resultHandler: { [weak self] (image, info) in
if let image = image {
self?.imageView.image = image
}
}) { [weak self] (progress, error, stop, info) in
self?.progressOfDownloadingInCloud = progress
print("downloading progress of icloud photo: \(String(progress))")
}
} else {
self.photoAsset.requestMaxSizeImage { [weak self] (image, info) in
if let image = image {
self?.imageView.image = image
}
}
}
保存或删除图片
_ = SwiftyPhotos.shared.saveImage(image, intoAlbum: "SwiftyPhotos", withLocation: nil) { (isImageSaved, nil) in
print("image saved: \(isImageSaved)")
}
_ = SwiftyPhotos.shared.deleteAsset(self.photoAsset) { (isAssetDeleted, error) in
print("asset deleted: \(isAssetDeleted)")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5, execute: {
self.dismiss(animated: true, completion: nil)
})
}
缩放图片视图
let v = ZoomImageView(frame: self.view.bounds)
self.photoAsset.requestMaxSizeImage { [weak self] (image, info) in
if let image = image {
self?.zoomImageView.image = image
}
}
联系方式
如果您发现任何问题,只需打开一个工单。欢迎提交拉取请求。
许可证
SwiftyPhotos 在 MIT 许可证下发布。详细信息请参阅 LICENSE.md。