Sanity

0.1.0

sanity-io/swift-sanity

What's New

2021-05-06T17:57:16Z

First release, focusing on asset pipeline URL generation for conserving hotspot and crop information

Swift Sanity

Code for interacting with Sanity content from Swift on iOS and macOS. This package is under development and may long term aim for feature parity with the JavaScript client, but for the time being is limited to Sanity asset pipeline URL generation.

Generate image asset URLs

This module provides SanityType.Image which when put through imageURL on SanityClient will generate image asset URLs which respect any hotspot or crop rect set by editors.

let client = SanityClient(projectId: "zp7mbokg", dataset: "production")
let image = SanityType.Image(
    asset: SanityType.Ref(
        _ref: "image-Tb9Ew8CXIwaY6R1kjMvI0uRR-2000x3000-jpg",
        _type: "reference"
    ),
    crop: SanityType.Image.Crop(
        bottom: 0.1,
        left: 0.1,
        right: 0.1,
        top: 0.1
    ),
    hotspot: SanityType.Image.Hotspot(
        height: 0.3,
        width: 0.3,
        x: 0.3,
        y: 0.3
    )
)
let url = client.imageURL(image)
    .width(30)
    .height(100)
    .URL()

// => "https://cdn.sanity.io/images/zp7mbokg/production/Tb9Ew8CXIwaY6R1kjMvI0uRR-2000x3000.jpg?rect=240,300,720,2400&w=30&h=100"

Note that a SanityClient is used here but this operation will not result in a network call. The projectId and dataset name is needed to generate asset URL.

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

Last updated: Mon Apr 22 2024 05:03:37 GMT-0900 (Hawaii-Aleutian Daylight Time)