PhotosPreview 1.3.8

PhotosPreview 1.3.8

Ciao Huang 维护。



  • 作者:
  • Ciao Huang

PhotosPreview

  • 类似 Messenger 的照片浏览应用,让用户预览和选择照片。
  • PhotosPreview 可完全自定义。
  • 您可以使用 PhotosPreview 替代 UIImagePickerController。
  • 使用 Swift 开发。

预览

特点

  • UIImagePickerController 替代品
  • 可自定义照片宽高比、预览栏高度和每行单元格数量
  • 启用相册选择

安装

手动安装

下载并将 'PhotosPreview' 文件夹拖放到您的 Xcode 项目中。

使用 CocoaPods

在您的 Podfile 文件中添加 pod 'PhotosPreview',然后运行 pod install

use_frameworks!
pod 'PhotosPreview'

快速开始

导入 PhotosPreview 库 import PhotosPreview,然后使用以下代码并将 PhotoGridDelegate 分配给视图控制器。

let photoGridViewController = PhotoGridViewController(nibName: PhotoGridViewController.nibName, bundle: Bundle(for: PhotoGridViewController.self))
photoGridViewController.delegate = self
self.present(photoGridViewController, animated: true, completion: nil)

然后,您可以在下方的 PhotoGridDelegate 方法中获取 PhotoGridController 中所选的图片。

func didSelectImage(_ image: UIImage, by controller: PhotoGridViewController) {
    // Do something with the image
}

使用 PhotoPreviewBar

PhotoPreviewBar 是一个可从视图控制器底部打开和关闭的集合视图。使视图控制器符合 PhotoPreviewBarDelegate 协议,并添加以下代码。

// Setup PhotoPreviewBar
let frame = CGRect(x: 0, y: view.frame.maxY, width: view.frame.width, height: 150)
let previewBar = PhotoPreviewBar(frame: frame)
view.addSubview(previewBar)
previewBar.delegate = self

// Setup PhotoProvider
let photoProvider = PhotoProvider()
// can select a smart album to display in the preview bar
photoProvider.fetchAssets(in: .cameraRoll) 
previewBar.photoProvider = photoProvider

设置何时打开预览栏。例如,创建一个在按钮按下时被触发的函数。

// Setup PhotoPreviewBar
func previewButtonPressed() {
    if previewBar.isOpened {
        previewBar.close() // close PhotoPreviewBar to the bottom of superview
    } else {
        previewBar.open() // open PhotoPreviewBar from the bottom of superview
    }
}

您还可以通过 PhotoPreviewBarDelegate 方法获取选中的图片。

func didSeleteImage(_ image: UIImage, by previewBar: PhotoPreviewBar) {
    // Do something with the image
}

PhotoPreviewBar 设计时包含一个按钮,可以用来呈现 PhotoGridViewController。如果您想通过这个默认按钮打开 PhotoGridViewController,只需将目标添加到 previewBar.photoGridButton,并呈现 PhotoGridViewController。如果不需要此按钮,只需将 isHidden 设置为 true。

委托方法

PhotoGridDelegate 用于使用 PhotoGridViewController

// Required
func didSelectImage(_ image: UIImage, by controller: PhotoGridViewController)

// Optional
func photoGridViewDidLoad()
func photoGridViewWillApear()
func photoGridViewDidApear()
func photoGridWillDismiss()
func photoGridDidDismissed()

PhotoPreviewBarDelegate 用于使用 PhotoPreviewBar

// Required
func didSeleteImage(_ image: UIImage, by previewBar: PhotoPreviewBar)

// Optional
// called before the animation of opening preview bar
func previewBarWillOpen() 
// called after the animation of opening preview bar
func previewBarDidOpen() 
// called before the animation of closing preview bar
func previewBarWillClose() 
// called after the animation of closing preview bar
func previewBarDidClose() 

如何自定义

let photoGridViewController = PhotoGridViewController(nibName: PhotoGridViewController.nibName, bundle: Bundle(for: PhotoGridViewController.self))
photoGridViewController.delegate = self
// ...
photoGridViewController.numberOfCellPerRow = 2
photoGridViewController.minimumLineSpacing = 1
photoGridViewController.aspectRatio = 0.6
// ...
self.presentViewController(photoGridViewController, animated: true, completion: nil)

属性

PhotoGridViewController 中声明。

属性 类型 描述 默认
numberOfCellPerRow CGFloat 每行细胞数量。 3
minimumInteritemSpacing CGFloat 集合视图流布局的最小单元间距离。 4
horizontalEdgeInset CGFloat 集合视图的左边距和右边距。 0
verticalEdgeInset CGFloat 集合视图的上边距和下边距。 0
aspectRatio CGFloat 照片网格细胞的宽高比。 1
thumbnailSize CGSize 照片网格细胞中的图像大小。 UIScreen.main.scale * <CellSize>
backgroundColor UIColor 集合视图的背景颜色。 黑色

PhotoPreviewBar 中声明。

属性 类型 描述 默认
minimumLineSpacing CGFloat 集合视图流布局的最小行间距。 2
minimumInteritemSpacing CGFloat 集合视图流布局的最小单元间距离。 0
horizontalEdgeInset CGFloat 集合视图的左边距和右边距。 0
verticalEdgeInset CGFloat 集合视图的上边距和下边距。 0
aspectRatio CGFloat 照片网格细胞的宽高比。 1
thumbnailSize CGSize 照片网格细胞中的图像大小。 UIScreen.main.scale * <CellSize>
animationDuration TimeInterval 打开和关闭预览条动画的时间间隔。 0.3
barBackgroundColor UIColor 集合视图的背景颜色。 clear

作者

黄超 [email protected]

许可证

PhotosPreview 允许在 MIT 许可证下发布。