首页 > 解决方案 > 使用 AVAssetWriter 的 UIImages to Video 为 < 800 个图像产生黑框闪烁

问题描述

我被困了好几个星期!

我在stackoverflow上尝试了所有解决方案,它们在大约0到500张图像或12 fps上50秒的情况下工作正常,但对于更长的视频,大约50秒后会出现黑色闪烁,具体取决于iphone,iphone 6在50后发生在 iphone 8 上秒它发生在 1:30 之后

我尝试了从这个链接 writeImagesAsMovie()功能接受的答案

有时我遇到内存分配问题,有时我没有,但黑色闪烁仍然存在。

 // MARK: - Fill Pixel Buffer -

private func fillPixelBufferFromImage(image: UIImage, pixelBuffer: CVPixelBuffer) {
    CVPixelBufferLockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))
    let pixelData = CVPixelBufferGetBaseAddress(pixelBuffer)
    let rgbColorSpace = CGColorSpaceCreateDeviceRGB()

    // Create CGBitmapContext
    let context = CGContext(
      data: pixelData,
      width: Int(720),
      height: Int(720),
      bitsPerComponent: 8,
      bytesPerRow: CVPixelBufferGetBytesPerRow(pixelBuffer),
      space: rgbColorSpace,
      bitmapInfo: CGImageAlphaInfo.premultipliedFirst.rawValue
    )


    // Draw image into context"
    context?.draw(image.cgImage!, in: CGRect(x: 0, y: 0, width: 720, height: 720))

    CVPixelBufferUnlockBaseAddress(pixelBuffer, CVPixelBufferLockFlags(rawValue: 0))


    //UIGraphicsEndImageContext()
}

标签: iosswiftavfoundation

解决方案


有助于获得所需结果的事情......使用 UIImageJPEGRepresentation 而不是 PNG,尺寸必须是 16 的倍数...... :) 并且较新的 iphone 可以处理更多图像


推荐阅读