Swiftpack.co -  Package - pixel-foundry/remote-image-dimensions
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
pixel-foundry/remote-image-dimensions
📐Swift package for determining the dimensions of a remote image without downloading the whole thing
.package(url: "https://github.com/pixel-foundry/remote-image-dimensions.git", from: "1.1.5")

RemoteImageDimensions

RemoteImageDimensions is a Swift package for determining the dimensions and image type of a remote image without downloading the whole thing. The size of most image formats can be determined after downloading fewer than 30 bytes.

RemoteImageDimensions supports any platform that can run Swift executables: iOS, macOS, Linux, Windows.

Usage

import RemoteImageDimensions

let image = URL(string: "https://pixelfoundry.io/static/logo.png")!

RemoteImage.dimensions(of: image) { result in
    switch result {
        case .success(let dimensions):
            print(dimensions) // Dimensions(width: 606, height: 216, format: .png, bytes: 25)
        case .failure: return
    }
}

Combine Interface

RemoteImageDimensions offers Combine bindings on supported platforms.

import Combine
import RemoteImageDimensions

var cancellable: AnyCancellable?

cancellable = RemoteImage.dimensions(of: image).sink(
    receiveCompletion: { _ in },
    receiveValue: { dimensions in
        print(dimensions) // Dimensions(width: 606, height: 216, format: .png, bytes: 25)
    }
)

Configuration

You can specify the timeout and byte-range headers of network requests using RemoteImage.Configuration. RemoteImageDimensions attempts to download the absolute minimum amount of data to determine the size of the image, which may not always be enough data in the case of non-deterministic formats like JPEG.

RemoteImage.dimensions(of: image, configuration: .init(timeout: 10, byteRange: 0..<100000))

Installation

Swift Package Manager

dependencies: [
    .package(url: "https://github.com/pixel-foundry/remote-image-dimensions", from: "1.0.0")
],
targets: [
    .target(name: "YourTarget", dependencies: ["RemoteImageDimensions"])
]

GitHub

link
Stars: 2
Last commit: 3 weeks ago

Release Notes

1.1.5
3 weeks ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco