Adam Jensen did a fantastic job of creating some quick code to convert an image to mov.
https://gist.github.com/acj/6ae90aa1ebb8cad6b47b
All of the above works fine, but I would like to work with a device with a little more processors and some real storage than your average iPad, so I need it to work under OS X. Itβs easier said than done.
At first I did not receive UIKit, replaced it with Cocoa. No UIImage, replaced by NSImage. Making some corrections to the dark corrections; fixed / skipped reduced 31 errors to 5. But I need 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 downloaded CGIImage directly to the 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 help understanding how to extract pixels in this to populate pixelBuffer as I think. Where will I start to do this?