UnsplashSwiftUI

这个 SwiftUI 包使得在应用程序中使用 Unsplash API 变得简单易用。

像使用其他视图一样调用该视图并应用修饰符。该包将从 API 获取图像元数据,并异步加载远程图像。 该视图的左下角还会显示一个文本,用于感谢摄影师,并热链接到 Unsplash 上的图像(使用 API 的一项要求)。

要求

目前,该包仅与 iOS 15 和 iPadOS 15 兼容。 该包的早期版本支持较旧的操作系统版本,但此版本使用了新的并发特性以及新的 AsyncImage 视图。

该包需要 Unsplash API 的访问密钥(Client ID)。 您可以从 Unsplash 开发者页面获取。 您需要在那里注册您的应用程序,并且在您申请大容量应用程序之前,您将受到速率限制(每小时 API 请求的数量有限)(有关更多详细信息,请参阅 Unsplash 开发者页面)。

免责声明:到目前为止,我尚未尝试使用此库申请大容量应用程序。 我相信我已经满足了 API 的所有要求,您应该不会遇到任何问题,但我不能完全确定。

安装

在 Xcode 中,转到 File -> Add Packages 并粘贴存储库的 URL:https://github.com/ArnavMotwani/UnsplashSwiftUI.git 然后选择一个版本或主分支(我会更频繁地更新主分支,进行较小的更改,而版本号只会随着重大更改而增加)。

用法

使用 import UnsplashSwiftUI 将包导入到文件中,然后在您想要的任何地方调用 UnsplashRandom 视图。

示例

import SwiftUI
import UnsplashSwiftUI

struct UnsplashRandomTest: View {
    var body: some View {
        UnsplashRandom(clientId: "YOUR_ACCESS_KEY")
    }
}

struct UnsplashRandomTest_Previews: PreviewProvider {
    static var previews: some View {
        UnsplashRandomTest()
    }
}
import SwiftUI
import UnsplashSwiftUI

struct UnsplashRandomTest: View {
    var body: some View {
        UnsplashRandom(clientId: "YOUR_ACCESS_KEY")
        .padding(25)
    }
}

struct UnsplashRandomTest_Previews: PreviewProvider {
    static var previews: some View {
        UnsplashRandomTest()
    }
}
import SwiftUI
import UnsplashSwiftUI

struct UnsplashRandomTest: View {
    var body: some View {
        UnsplashRandom(clientId: "YOUR_ACCESS_KEY")
        .clipShape(RoundedRectangle(cornerRadius: 10))
    }
}

struct UnsplashRandomTest_Previews: PreviewProvider {
    static var previews: some View {
        UnsplashRandomTest()
    }
}

自定义

参数 可选? 类型 描述 默认值
clientId 字符串 来自 Unsplash 开发者页面的 Client ID -
orientation Orientations 按照片方向过滤。(有效值:.landscape, .portrait, .squarish, .none) -
query 字符串 将选择范围限制为与搜索词匹配的照片。 -
textColor 颜色 热链接到 Unsplash 上图像的文本颜色 Color.white
textBackgroundColor 颜色 文本背景颜色(不透明度自动设置为 0.2) Color.black
aspectRatio ContentMode 选择图像是适合还是填充容器 ContentMode.fit

示例

orientation

    UnsplashRandom(clientId: "YOUR_ACCESS_KEY", orientation: .landscape)
    
    UnsplashRandom(clientId: "YOUR_ACCESS_KEY", orientation: .portrait)
    
    UnsplashRandom(clientId: "YOUR_ACCESS_KEY", orientation: .squarish)

query

    UnsplashRandom(clientId: "YOUR_ACCESS_KEY", query: "trees")
    
    UnsplashRandom(clientId: "YOUR_ACCESS_KEY", query: "landscapes")
    
    UnsplashRandom(clientId: "YOUR_ACCESS_KEY", query: "space")

textColor

    UnsplashRandom(clientId: "YOUR_ACCESS_KEY", textColor: .black)
    
    UnsplashRandom(clientId: "YOUR_ACCESS_KEY", textColor: .blue)
    
    UnsplashRandom(clientId: "YOUR_ACCESS_KEY", textColor: .primary)

textBackgroundColor

    UnsplashRandom(clientId: "YOUR_ACCESS_KEY", textBackgroundColor: .white)
    
    UnsplashRandom(clientId: "YOUR_ACCESS_KEY", textBackgroundColor: .blue)
    
    UnsplashRandom(clientId: "YOUR_ACCESS_KEY", textBackgroundColor: .primary)

aspectRatio

    UnsplashRandom(clientId: "YOUR_ACCESS_KEY", aspectRatio: .fit)
    
    UnsplashRandom(clientId: "YOUR_ACCESS_KEY", aspectRatio: .fill)