Adam Jensen has done a fantastic job @ crafting some swift code to convert an image into a mov.
https://gist.github.com/acj/6ae90aa1ebb8cad6b47b
The above works great, but I would like to do the job with device with slightly more CPU and some real storage than your average iPad so I need this to run under OS X. Easier said than done.
First I got no UIKit, replaced with Cocoa. No UIImage, replaced with NSImage. Making some stab in the dark corrections; fixed/skipped reduced 31 errors down to 5. But I need some help with this bit.
func fillPixelBufferFromImage(image: NSImage, 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
switch(contentMode) {
case .ScaleAspectFill:
ratio = max(horizontalRatio, verticalRatio)
case .ScaleAspectFit:
ratio = min(horizontalRatio, verticalRatio)
default:
ratio = min(horizontalRatio, verticalRatio)
}
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
CGContextDrawImage(context, CGRectMake(x, y, newSize.width, newSize.height), image.CGImage)
CVPixelBufferUnlockBaseAddress(pixelBuffer, 0)
}
I got my CGIImage
loaded directly with Quartz2D
library.
let realURL = NSURL(string: urlString)
if let image = CGImageSourceCreateWithURL(realURL!, nil) {
var pixelBuffer: CVPixelBuffer? = nil
let status: CVReturn = CVPixelBufferPoolCreatePixelBuffer(kCFAllocatorDefault, pixelBufferAdaptor.pixelBufferPool!, &pixelBuffer)
if let pixelBuffer = pixelBuffer where status == 0 {
let managedPixelBuffer = pixelBuffer
But I need some help in understanding how to extract the pixels in this to populate the pixelBuffer
me thinks. Where do I start to do that?