gpt4 book ai didi

ios - AVCaptureStillImageOutput.pngStillImageNSDataRepresentation?

转载 作者:搜寻专家 更新时间:2023-11-01 06:41:34 27 4
gpt4 key购买 nike

我是第一次使用 AVCaptureStillImageOutput,我在某个时候保存了一张 JPEG 图像。我想保存 PNG 图像而不是 JPEG 图像。我需要为此做什么?

我在应用程序中有这 3 行代码:

let stillImageOutput = AVCaptureStillImageOutput()
stillImageOutput.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)

是否有一种简单的方法来修改这些行以获得我想要的内容?上网查了一下好像答案是NO(除非我不够幸运),不过我还是相信一定有好的解决办法。

最佳答案

AVFoundation Programming Guide 中有示例代码展示了如何将 CMSampleBuffer 转换为 UIImage(在 Converting CMSampleBuffer to a UIImage Object 下)。从那里,您可以使用 UIImagePNGRepresentation(image) 将其编码为 PNG 数据。

这是该代码的 Swift 翻译:

extension UIImage
{
// Translated from <https://developer.apple.com/library/ios/documentation/AudioVideo/Conceptual/AVFoundationPG/Articles/06_MediaRepresentations.html#//apple_ref/doc/uid/TP40010188-CH2-SW4>
convenience init?(fromSampleBuffer sampleBuffer: CMSampleBuffer)
{
guard let imageBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return nil }

if CVPixelBufferLockBaseAddress(imageBuffer, kCVPixelBufferLock_ReadOnly) != kCVReturnSuccess { return nil }
defer { CVPixelBufferUnlockBaseAddress(imageBuffer, kCVPixelBufferLock_ReadOnly) }

let context = CGBitmapContextCreate(
CVPixelBufferGetBaseAddress(imageBuffer),
CVPixelBufferGetWidth(imageBuffer),
CVPixelBufferGetHeight(imageBuffer),
8,
CVPixelBufferGetBytesPerRow(imageBuffer),
CGColorSpaceCreateDeviceRGB(),
CGBitmapInfo.ByteOrder32Little.rawValue | CGImageAlphaInfo.PremultipliedFirst.rawValue)

guard let quartzImage = CGBitmapContextCreateImage(context) else { return nil }
self.init(CGImage: quartzImage)
}
}

关于ios - AVCaptureStillImageOutput.pngStillImageNSDataRepresentation?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34605236/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com