31 lines
809 B
Swift
31 lines
809 B
Swift
|
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)
|
||
|
}
|
||
|
}
|