我在此链接https://github.com/justinlevi/imagesToVideo中修改了时间推移构建器类中的一些代码,以接收图像数组而不是网址。
就拍摄图像和制作视频而言,一切似乎都很好;但是,我在视频中显示图像的方式存在一些问题。
我觉得问题出在fillPixelBufferFromImage方法中:
func fillPixelBufferFromImage(image: UIImage, pixelBuffer: CVPixelBuffer, contentMode:UIViewContentMode){
CVPixelBufferLockBaseAddress(pixelBuffer, 0)
let data = CVPixelBufferGetBaseAddress(pixelBuffer)
let rgbColorSpace = CGColorSpaceCreateDeviceRGB()
let context = CGBitmapContextCreate(data, Int(self.outputSize.width), Int(self.outputSize.height), 8, CVPixelBufferGetBytesPerRow(pixelBuffer), rgbColorSpace, CGImageAlphaInfo.PremultipliedFirst.rawValue)
CGContextClearRect(context, CGRectMake(0, 0, CGFloat(self.outputSize.width), CGFloat(self.outputSize.height)))
let horizontalRatio = CGFloat(self.outputSize.width) / image.size.width
let verticalRatio = CGFloat(self.outputSize.height) / image.size.height
var ratio: CGFloat = 1
// print("horizontal ratio \(horizontalRatio)")
// print("vertical ratio \(verticalRatio)")
// print("ratio \(ratio)")
// print("Image Width - \(image.size.width). Image Height - \(image.size.height)")
switch(contentMode) {
case .ScaleAspectFill:
ratio = max(horizontalRatio, verticalRatio)
case .ScaleAspectFit:
ratio = min(horizontalRatio, verticalRatio)
default:
ratio = min(horizontalRatio, verticalRatio)
}
//print("after ratio \(ratio)")
let newSize:CGSize = CGSizeMake(image.size.width * ratio, image.size.height * ratio)
let x = newSize.width < self.outputSize.width ? (self.outputSize.width - newSize.width) / 2 : 0
let y = newSize.height < self.outputSize.height ? (self.outputSize.height - newSize.height) / 2 : 0
//print("x \(x)")
//print("y \(y)")
CGContextDrawImage(context, CGRectMake(x, y, newSize.width, newSize.height), image.CGImage)
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0)
}
请指教。谢谢
编辑:有人可以帮忙吗?我非常坚持让这些图像在视频中显示,就像他们拍摄时一样。谢谢你的帮助