DivingBoard 1.1.4

DivingBoard 1.1.4

Jim Rhoades维护。




DivingBoard

一个iOS框架,提供从Unsplash.com浏览和搜索照片的界面。

Platform Swift version License Carthage compatible Version

DivingBoard

试用DivingBoard

这个仓库包含一个名为“PhotoViewer”的示例应用程序,它使用DivingBoard框架(观看这个简短视频。)

PhotoViewer

为了运行它,您首先需要注册一个Unsplash应用程序ID

一旦您有了Unsplash应用程序ID,克隆或下载此仓库。然后打开Xcode中的DivingBoard/ExampleApp/PhotoViewer/PhotoViewer.xcodeproj,编辑ViewController.swift文件,将ID添加到顶部出现的位置
// let unsplashAppID = "INSERT_YOUR_APPLICATION_ID_HERE"

(如果您计划为DivingBoard做贡献,请参阅下文的"为DivingBoard做贡献",这样会将您的Unsplash应用程序ID从提交中排除。)

然后您可以运行PhotoViewer来查看DivingBoard是如何工作的。

通过Carthage安装

  1. 如果您还没有安装Carthage,请安装Carthage
  2. 在您的项目基本目录中创建一个名为"Cartfile"的文件,包含 github "jim-rhoades/DivingBoard"
  3. 在终端运行carthage update(在您创建Cartfile的同一目录中)。这将编译框架并将其放置在您的项目文件夹中的Carthage/Build/iOS/DivingBoard.framework中。
  4. 打开您的项目在Xcode中,查看应用程序目标的“通用”设置选项卡,滚动到“已封装的二进制文件”部分。将DivingBoard.framework文件拖放到“已封装的二进制文件”部分,当提示时点击“完成”。

DivingBoard Xcode setup 1

  1. 在您的应用程序目标的“构建阶段”选项卡中,点击"+"按钮添加一个新的构建阶段,并选择“新建运行脚本阶段”。
  2. 将以下内容添加到shell下方的脚本区域中:/usr/local/bin/carthage copy-frameworks
  3. 在“输入文件”下添加:$(SRCROOT)/Carthage/Build/iOS/DivingBoard.framework
  4. 在“输出文件”下添加:$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/DivingBoard.framework

DivingBoard Xcode setup 2

通过CocoaPods安装

  1. 您可以通过将DivingBoard添加到Podfile中来使用CocoaPods进行安装
platform :ios, '10.0'
pod 'DivingBoard', '~> 1.1.4'
  1. 在终端中,导航到Podfile所在的同一个目录,并运行pod install
  2. 请记住,请用.xcworkspace文件而不是原始的.xcodeproj文件打开您的Xcode项目。

在您的应用中启动DivingBoard

要启动自己的应用中的DivingBoard,您需要获取一个UnsplashPickerViewController的实例并展示出来。以下代码显示了如何将其全屏展示:

let unsplashAppID = "INSERT_YOUR_APPLICATION_ID_HERE"
let unsplashPicker = DivingBoard.unsplashPicker(withClientID: unsplashAppID,
                                                              presentingViewController: self,
                                                              modalPresentationStyle: .fullScreen)

present(unsplashPicker, animated: true, completion: nil)

在PhotoViewer的ViewController.swift中,您还可以找到将其视为弹出视图和将其推送到 UINavigationController 栈中的示例。

遵从UnsplashPickerDelegate

向您展示DivingBoard的视图控制器还需要遵从UnsplashPickerDelegate协议,它包含两个方法

func unsplashPickerDidCancel()
func unsplashPickerDidFinishPicking(photo: UnsplashPhoto)

参见PhotoViewer的ViewController.swift文件中的示例。

遵从Unsplash API规则

您需要自己“钓鱼愉快”并遵从Unsplash API规则

DivingBoard包含一些方法,使遵从“技术规则”部分变得简单

  • 当需要执行类似保存照片到相册的操作时,请调用 DivingBoard.incrementUnsplashPhotoDownloadCount 方法。此方法会调用Unsplash API的 download_location 终端,以增加Unsplash.com上照片的下载次数。强烈建议阅读 Unsplash API指南:触发下载,获取关于何时进行此操作的推荐。(PhotoViewer在保存照片到相册时会使用此方法。)

  • 当在Unsplash.com上链接照片或用户时,请调用 DivingBoard.unsplashWebsiteURLWithReferral 来获取包含适当归属描述的URL,具体请参阅 Unsplash API指南:归属。(PhotoViewer在点击照片或用户头像以查看Unsplash网站上的照片/用户时使用此方法。)

有用的工具

DivingBoard包含对一个UIImageView扩展,该方法名为 loadImageAsync,会异步加载并缓存图像到内存/磁盘。

还包括一个名为 LoadingView 的类,它是一个很棒的加载指示器。

您可能需要在您自己的应用程序中实现 unsplashPickerDidFinishPicking 时使用这两个之一或全部使用,例如

func unsplashPickerDidFinishPicking(photo: UnsplashPhoto) {
    // show a loading indicator
    let loadingView = LoadingView()
    photoView.addCenteredSubview(loadingView)
        
    // load the photo
    let photoURL = photo.urls.full
    photoView.loadImageAsync(with: photoURL) { success in
        // remove the loading indicator
        loadingView.removeFromSuperview()
    }

    dismiss(animated: true, completion: nil)
}

为DivingBoard做出贡献

如果您想为DivingBoard做出贡献,我建议您在PhotoViewer Xcode项目中创建一个名为"ClientID.swift"的文件,并包含以下内容(将"YOUR_UNSPLASH_APP_ID"替换为您实际的Unsplash应用ID)

import Foundation

// Note that this file is in .gitignore and will NOT be added to the repository.
// (to prevent the app ID from leaking onto GitHub!)
let unsplashAppID = "YOUR_UNSPLASH_APP_ID"

由于"ClientID.swift"已包含在.gitignore中,这样您就可以在运行和测试更改DivingBoard和PhotoViewer时,避免意外将您的Unsplash应用ID包含到提交中。

待办事项

  • 将网格布局从正方形布局更改为瀑布流布局,展示未裁剪的照片而非方形裁剪: DivingBoard
  • 改进单元测试 - 特别是应该对UnsplashClient的 requestPhotosFor 方法使用存根/伪数据进行测试(该项目中包含包含从Unsplash API获取的实时JSON数据的文件… test_data_photos.jsontest_data_search.json,但目前尚未使用)