DivingBoard
一个iOS框架,提供从Unsplash.com浏览和搜索照片的界面。
试用DivingBoard
这个仓库包含一个名为“PhotoViewer”的示例应用程序,它使用DivingBoard框架(观看这个简短视频。)
为了运行它,您首先需要注册一个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安装
- 如果您还没有安装Carthage,请安装Carthage。
- 在您的项目基本目录中创建一个名为"Cartfile"的文件,包含
github "jim-rhoades/DivingBoard"
- 在终端运行
carthage update
(在您创建Cartfile的同一目录中)。这将编译框架并将其放置在您的项目文件夹中的Carthage/Build/iOS/DivingBoard.framework
中。 - 打开您的项目在Xcode中,查看应用程序目标的“通用”设置选项卡,滚动到“已封装的二进制文件”部分。将
DivingBoard.framework
文件拖放到“已封装的二进制文件”部分,当提示时点击“完成”。
- 在您的应用程序目标的“构建阶段”选项卡中,点击"+"按钮添加一个新的构建阶段,并选择“新建运行脚本阶段”。
- 将以下内容添加到shell下方的脚本区域中:
/usr/local/bin/carthage copy-frameworks
- 在“输入文件”下添加:
$(SRCROOT)/Carthage/Build/iOS/DivingBoard.framework
- 在“输出文件”下添加:
$(BUILT_PRODUCTS_DIR)/$(FRAMEWORKS_FOLDER_PATH)/DivingBoard.framework
通过CocoaPods安装
- 您可以通过将DivingBoard添加到Podfile中来使用CocoaPods进行安装
platform :ios, '10.0'
pod 'DivingBoard', '~> 1.1.4'
- 在终端中,导航到Podfile所在的同一个目录,并运行
pod install
。 - 请记住,请用
.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包含到提交中。