Collection of UIImage
utility methods to ease integration with media frameworks.
- iOS 13.0, macOS 10.15
- Swift 5.2
Use Xcode's built-in Swift Package Manager:
- Open Xcode
- Click File -> Swift Packages -> Add Package Dependency
- Paste package repository https://github.com/frolovilya/UIImageExtensions.git and press return
- Configure dependency version settings
- Import module to any file using
import UIImageExtensions
import UIKit
import UIImageExcensions
// obtain some UIImage instance
let myImage: UIImage = #imageLiteral(resourceName: "MyImage")
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)
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)
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