Swiftpack.co - frolovilya/UIImageExtensions as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by frolovilya.
frolovilya/UIImageExtensions 0.2.0
Various UIImage utility methods to ease integration with media frameworks
⭐️ 5
🕓 3 years ago
iOS macOS
.package(url: "https://github.com/frolovilya/UIImageExtensions.git", from: "0.2.0")

UIImageExtensions

Collection of UIImage utility methods to ease integration with media frameworks.

Requirements

  • iOS 13.0, macOS 10.15
  • Swift 5.2

Installation

Use Xcode's built-in Swift Package Manager:

Usage

import UIKit
import UIImageExcensions

// obtain some UIImage instance
let myImage: UIImage = #imageLiteral(resourceName: "MyImage")

Core Video pixel buffer

Copies image data to CVPixelBuffer.

// func toPixelBuffer() -> CVPixelBuffer?

let buffer: CVPixelBuffer? = myImage.toPixelBuffer()

And back from CVPixelBuffer to UIImage. Provide optional orientation parameter to rotate image.

// func toImage(orientation: CGImagePropertyOrientation = .up) -> UIImage?

let backToImage: UIImage? = buffer?.toImage(orientation: .left)

Core Media sample buffer

Actually wraps CVPixelBuffer to CMSampleBuffer with additional sample timing information.

With provided frameIndex and framesPerSecond the following timing rules apply:

  • Sample duration (seconds): 1 / framesPerSecond
  • Sample start time: frameIndex / framesPerSecond

Assuming that timing starts from zero.

// func toSampleBuffer(frameIndex: Int = 0, framesPerSecond: Double = 24) -> CMSampleBuffer?

let buffer: CMSampleBuffer? = myImage.toSampleBuffer(frameIndex: 1, framesPerSecond: 24)

Create UIImage from Base64 encoded string

Convenience static initializer to get UIImage instance right from Base64 encoded string.

// static func fromBase64String(_ base64String: String) -> UIImage?

let decodedImage: UIImage? = UIImage.fromBase64String("data:image/jpeg;base64,/9j/4AAQSkZJ...")

And property containing Base64 string of a UIImage instance.

// var base64String: String?

let imageAsString: String? = myImage.base64String

GitHub

link
Stars: 5
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Release Notes

0.2.0
3 years ago
  • OSX support
  • Removed video generation code
  • Fixes to run on a real device

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