美文网首页
CVPixelBuffer 与 UIImage 相互转换

CVPixelBuffer 与 UIImage 相互转换

作者: 熊啊熊啊熊 | 来源:发表于2019-11-26 16:23 被阅读0次

直接上代码:

extension UIImage {
    
    /// 重绘图片大小
    func resizedImage(outputSize: CGSize) -> UIImage? {
        if size == outputSize {
            return self
        }else {
            UIGraphicsBeginImageContext(outputSize)
//            UIGraphicsBeginImageContextWithOptions(outputSize, false, 0.0)
            draw(in: CGRect(x: 0, y: 0, width: outputSize.width, height: outputSize.height))
            let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            guard let newImage = scaledImage else {
                return nil
            }
            return newImage
        }
    }
    
    /// UIImage -> CVPixelBuffer
    func imageToPixelBuffer(outputSize: CGSize) -> CVPixelBuffer? {
        
        let inputImage: UIImage = self
        var pixelBuffer: CVPixelBuffer? = nil
        guard let cgImage: CGImage = inputImage.cgImage else {
            return pixelBuffer
        }
        /// 分配内存,创建CVPixelBuffer
        let status = CVPixelBufferCreate(kCFAllocatorDefault, Int(outputSize.width), Int(outputSize.height), kCVPixelFormatType_32ARGB, nil, &pixelBuffer)
        if status == kCVReturnSuccess, let pixelBuffer = pixelBuffer {
            /// 写入数据
            let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
            CVPixelBufferLockBaseAddress(pixelBuffer, .readOnly)
            let context = CGContext(data: CVPixelBufferGetBaseAddress(pixelBuffer),
                                    width: Int(outputSize.width),
                                    height: Int(outputSize.height),
                                    bitsPerComponent: 8,
                                    bytesPerRow: bytesPerRow,
                                    space: CGColorSpaceCreateDeviceRGB(),
                                    bitmapInfo: CGBitmapInfo.byteOrder32Little.rawValue | CGImageAlphaInfo.noneSkipFirst.rawValue)
            context?.draw(cgImage, in: CGRect(x: 0, y: 0, width: outputSize.width, height: outputSize.height))
            CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly)
            return pixelBuffer
        }
        return pixelBuffer
    }
    
    /// CVPixelBuffer -> UIImage
    class func pixelBufferToImage(pixelBuffer: CVPixelBuffer, outputSize: CGSize) -> UIImage? {
//        let type = CVPixelBufferGetPixelFormatType(pixelBuffer)
        
        let width = CVPixelBufferGetWidth(pixelBuffer)
        let height = CVPixelBufferGetHeight(pixelBuffer)
        let bytesPerRow = CVPixelBufferGetBytesPerRow(pixelBuffer)
        
        CVPixelBufferLockBaseAddress(pixelBuffer, .readOnly)
        guard let context = CGContext(data: CVPixelBufferGetBaseAddress(pixelBuffer),
                                      width: width,
                                      height: height,
                                      bitsPerComponent: 8,
                                      bytesPerRow: bytesPerRow,
                                      space: CGColorSpaceCreateDeviceRGB(),
                                      bitmapInfo: CGBitmapInfo.byteOrder32Little.rawValue | CGImageAlphaInfo.noneSkipFirst.rawValue),
            let imageRef = context.makeImage() else
        {
                return nil
        }
        
        let newImage = UIImage(cgImage: imageRef, scale: 1, orientation: UIImage.Orientation.up).resizedImage(outputSize: outputSize)
        CVPixelBufferUnlockBaseAddress(pixelBuffer, .readOnly)
        
        return newImage
    }
    
}

仅此记录
热爱生活,记录生活!

相关文章

网友评论

      本文标题:CVPixelBuffer 与 UIImage 相互转换

      本文链接:https://www.haomeiwen.com/subject/itcywctx.html