Caps-iOS/Caps/Extensions/CGImage+Extensions.swift

31 lines
809 B
Swift
Raw Normal View History

2022-06-10 21:20:49 +02:00
import CoreGraphics
import VideoToolbox
extension CGImage {
static func create(from cvPixelBuffer: CVPixelBuffer?) -> CGImage? {
guard let pixelBuffer = cvPixelBuffer else {
return nil
}
var image: CGImage?
VTCreateCGImageFromCVPixelBuffer(pixelBuffer, options: nil, imageOut: &image)
return image
}
/**
Crop an image to a square, centered around the middle of the frame
- parameter size: The height and width of the resulting image
- returns: The cropped image
*/
func crop(to size: CGFloat) -> CGImage? {
let rect = CGRect(
x: (CGFloat(height) - size) / 2,
y: (CGFloat(width) - size) / 2,
width: size,
height: size)
return cropping(to: rect)
}
}