I have a serious problem: I have an NSArray
with several UIImage
objects. What I now want to do, is create movie from those UIImages
. But I don't have any idea how to do so.
I hope someone can help me or send me a code snippet which does something like I want.
Edit: For future reference - After applying the solution, if the video looks distorted, make sure the width of the images/area you are capturing is a multiple of 16. Found after many hours of struggle here:
Why does my movie from UIImages gets distorted?
Here is the complete solution (just ensure width is multiple of 16)
http://codethink.no-ip.org/wordpress/archives/673
I took Zoul's main ideas and incorporated the AVAssetWriterInputPixelBufferAdaptor method and made the beginnings of a little frameworks out of it.
Feel free to check it out and improve upon it! CEMovieMaker
Here's a Swift 2.x version tested on iOS 8. It combines answers from @Scott Raposa and @Praxiteles along with code from @acj contributed for another question. The code from @acj is here: https://gist.github.com/acj/6ae90aa1ebb8cad6b47b. @TimBull also provided code as well.
Like @Scott Raposa, I had never even heard of
CVPixelBufferPoolCreatePixelBuffer
and several other functions, let alone understood how to use them.What you see below was cobbled together mostly by trial and error and from reading Apple docs. Please use with caution, and provide suggestions if there are mistakes.
Usage:
Code:
Take a look at AVAssetWriter and the rest of the AVFoundation framework. The writer has an input of type AVAssetWriterInput, which in turn has a method called appendSampleBuffer: that lets you add individual frames to a video stream. Essentially you’ll have to:
1) Wire the writer:
2) Start a session:
3) Write some samples:
4) Finish the session:
You’ll still have to fill-in a lot of blanks, but I think that the only really hard remaining part is getting a pixel buffer from a
CGImage
:frameSize
is aCGSize
describing your target frame size andframeTransform
is aCGAffineTransform
that lets you transform the images when you draw them into frames.Swift 4 version for macOS (not iOS), based on @Mikita Manko.
Usage:
Use AVAssetWriter to write images as movie. I already have answered here:- https://stackoverflow.com/a/19166876/1582217
Just translated @Scott Raposa answer to swift3 (with some very little changes):