我从这个答案 How do I export UIImage array as a movie?得到了代码
我把这个函数称为:
let size = CGSize(wIDth: 1280,height: 720)let pathVIDeo = NSSearchPathForDirectorIEsInDomains(.documentDirectory,.UserDomainMask,true)let percorsoVIDeo = pathVIDeo[0]writeImagesAsMovIE(arrayImmagini,vIDeoPath: percorsoVIDeo+"/prova.mp4",vIDeoSize: size,vIDeoFPS: 1)
“arrayImmagini”的字面定义如下:
var arrayImmagini = [UIImage(imageliteral: "Frames/turtle/turtle0.jpg"),UIImage(imageliteral: "Frames/turtle/turtle1.jpg"),...]
当我尝试运行代码时,我得到一个完全黑色的视频,而xcode给了我这两个错误的次数与数组中的数量一样多:
Sep 5 09:24:15 Prova[1554] <Error>: CGBitmapContextCreate: invalID data bytes/row: should be at least 7680 for 8 integer bits/component,3 components,kCGImageAlphaPremultiplIEdFirst.Sep 5 09:24:15 Prova[1554] <Error>: CGContextDrawImage: invalID context 0x0. If you want to see the backtrace,please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
阅读有关CGBitmapContextCreate的文档,我尝试以不同方式调用它:
func fillPixelBufferFromImage(image: UIImage,pixelBuffer: CVPixelBufferRef) { CVPixelBufferLockBaseAddress(pixelBuffer,0) let pixelData = CVPixelBufferGetBaseAddress(pixelBuffer) let rgbcolorSpace = CGcolorSpaceCreateDeviceRGB() // Create CGBitmapContext let context = CGBitmapContextCreate( nil,Int(image.size.wIDth),Int(image.size.height),8,rgbcolorSpace,CGImageAlphaInfo.PremultiplIEdFirst.rawValue ) // Draw image into context CGContextDrawImage(context,CGRectMake(0,image.size.wIDth,image.size.height),image.CGImage) CVPixelBufferUnlockBaseAddress(pixelBuffer,0)}
代替:
func fillPixelBufferFromImage(image: UIImage,0) let pixelData = CVPixelBufferGetBaseAddress(pixelBuffer) let rgbcolorSpace = CGcolorSpaceCreateDeviceRGB() // Create CGBitmapContext let context = CGBitmapContextCreate( pixelData,CVPixelBufferGetBytesPerRow(pixelBuffer),0)}
这使xcode停止给我错误,但我仍然得到一个黑色视频.
请帮助我,我是应用程序开发的新手,甚至更新的AVFoundation,我没有任何关于如何自己解决它的线索.
谢谢!
您不能将视频大小设置为比图片大小更小.
一旦我这样做,一切正常:
let size = CGSize(wIDth: 1920,height: 1280)总结
以上是内存溢出为你收集整理的ios – 如何在Swift中将图像导出为视频?全部内容,希望文章能够帮你解决ios – 如何在Swift中将图像导出为视频?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)