ZJPhotoBrowser 0.1.0

ZJPhotoBrowser 0.1.0

测试已测试
语言语言 SwiftSwift
许可 MIT
发布最新版本2016年5月
SPM支持SPM

ZeroJ维护。



  • 作者:
  • ZeroJ

PhotoBrowser


提供了一种简单的方法来实现类似系统相册查看图片的效果(快速集成图片浏览器,支持网络、本地以及各种手势)

使用示例效果

网络图片.gif 最终效果.gif


可以简单、快速、灵活地实现上图中的效果

注意,代码依赖Kingfisher,请先在您的项目中添加Kingfisher框架

注意,PhotoBrowser依赖'Kingfisher'


功能

  • 提供了一种简单的方法来实现类似系统相册查看图片的效果
  • 支持旋转

书写思路移步

简书1


要求

  • iOS 8.0+
  • Xcode 7.3 或更高版本

安装

  • 将以下行添加到项目文件'Podfile'中:

source 'https://github.com/CocoaPods/Specs.git'

platform :ios, '8.0' use_frameworks!

pod 'ZJPhotoBrowser', '~> 0.1.0'

  • 直接下载后,将PhotoBrowser文件夹下的文件拖到您的项目中使用
  • 或者将下载的文件'PhotoBrowser'拖到您的项目中

用法


一. 本地图片使用示例(加载本地图片的示例)

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    1. 设置photoModels
    func setPhoto() -> [PhotoModel] {
        var photos: [PhotoModel] = []
        for (index, image) in images.enumerate() {
            //  这个方法只能返回可见的cell, 如果不可见, 返回值为nil
            let cell = collectionView.cellForItemAtIndexPath(NSIndexPath(forRow: index, inSection: 0)) as? TestCell
            let sourceView = cell?.imageView
            // 在这里设置本地图片的photoModel, 注意指定的sourceImageView是图片的imageView,用来执行动画和设置初始图片
            let photoModel = PhotoModel(localImage: image, sourceImageView: sourceView, description: nil)
            photos.append(photoModel)
        }
        return photos
    }

    // 2. 初始化PhotoBrowser
    let photoBrowser = PhotoBrowser(photoModels: setPhoto())
    // 3. 设置初始的页数
    photoBrowser.showWithBeginPage(indexPath.row)
}

二. 网络图片使用(加载网络图片的示例)

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    1. 设置photoModels
    func setPhoto() -> [PhotoModel] {
        var photos: [PhotoModel] = []
        for (index, photoURLString) in photosURLString.enumerate() {
            // 这个方法只能返回可见的cell, 如果不可见, 返回值为nil
            let cell = collectionView.cellForItemAtIndexPath(NSIndexPath(forRow: index, inSection: 0)) as? TestCell
            let sourceView = cell?.imageView

            // 这里设置网络图片的photoModel 注意指定的sourceImageView是图片的imageView,用来执行动画和设置初始图片
            let photoModel = PhotoModel(imageUrlString: photoURLString, sourceImageView: sourceView, description: nil)
            photos.append(photoModel)
        }
        return photos
    }

    // 2. 初始化PhotoBrowser
    let photoBrowser = PhotoBrowser(photoModels: setPhoto())
    // 3. 设置初始的页数
    photoBrowser.showWithBeginPage(indexPath.row)
}

三. 使用代理,动态更新sourceImageView并进行更多操作(使用代理来完成更多有用的工作)

    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
    1. 设置photoModels
    func setPhoto() -> [PhotoModel] {
        var photos: [PhotoModel] = []
        for photoURLString in photosURLString {
            // 初始化不设置sourceImageView,也可以设置, 如果sourceImageView是不需要动态改变的, 那么推荐不需要代理设置sourceImageView
            // 而在代理方法中动态更新,将会覆盖原来设置的sourceImageView
            let photoModel = PhotoModel(imageUrlString: photoURLString, sourceImageView: nil, description: nil)
            photos.append(photoModel)
        }
        return photos
    }
    // 2. 初始化PhotoBrowser
    let photoBrowser = PhotoBrowser(photoModels: setPhoto())
    // 3. 指定代理
    photoBrowser.delegate = self
    // 4. 设置初始的页数
    photoBrowser.showWithBeginPage(indexPath.row)
}   

代理使用示例(代理方法使用示例)

// 正在展示的页
// 因为通过 collectionView.cellForItemAtIndexPath(currentIndexPath)
// 这个方法只能获取到当前显示在collectionView中的cell, 否则返回nil,
// 所以如果是在浏览图片的时候的页数对应的currentIndexPath已经不在collectionView的可见cell范围内
// 返回的将是nil, 就不能准确的获取到 sourceImageView 
// 故在每次显示图片的时候判断如果对应的sourceImageView cell已经不可见
// 那么就更新collectionView的位置, 滚动到相应的cell
// 当然更新collectionView位置的方法很多, 这里只是示例
func photoBrowserDidDisplayPage(currentPage: Int, totalPages: Int) {
    let visibleIndexPaths = collectionView.indexPathsForVisibleItems()

    let currentIndexPath = NSIndexPath(forRow: currentPage, inSection: 0)
    if !visibleIndexPaths.contains(currentIndexPath) {
        collectionView.scrollToItemAtIndexPath(currentIndexPath, atScrollPosition: .Top, animated: false)
    }
}
// 获取动态改变的sourceImageView
func sourceImageViewForCurrentIndex(index: Int) -> UIImageView? {

    let currentIndexPath = NSIndexPath(forRow: index, inSection: 0)

    let cell = collectionView.cellForItemAtIndexPath(currentIndexPath) as? TestCell
    let sourceView = cell?.imageView
    return sourceView
}

如果您在使用过程中遇到问题,请联系我

QQ:854136959 邮箱:[email protected]

如果对您有帮助,请随手给个star鼓励一下

许可

PhotoBrowser在MIT许可下发布。有关详情,请参阅LICENSE文件。