IOS 5:UIView转换为视频,结果视频已损坏

时间:2012-01-22 21:06:05

标签: ios multithreading image ios5 core-graphics

我想获取UIView,转换为图像,然后将该图像存储在视频文件(.mp4)中。 我使用下一部分代码来抓取图像并将其放入像素缓冲区:

BOOL appended;
    if(input.readyForMoreMediaData==YES){
        //grab the view and convert it into image
        CGSize imgsize=self.imageSource.frame.size;
        UIGraphicsBeginImageContext(imgsize);
        [self.imageSource.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage* grabbedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        CVReturn cvErr = kCVReturnSuccess;
        CGImageRef image = (CGImageRef) [grabbedImage CGImage];

        NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                                 [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
                                 [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
                                 nil];
        CVPixelBufferRef pxbuffer = NULL;

        CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, imgsize.width,
                                              imgsize.height, kCVPixelFormatType_32ARGB, (__bridge CFDictionaryRef) options, 
                                              &pxbuffer);

        NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);

        CVPixelBufferLockBaseAddress(pxbuffer, 0);
        void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
        NSParameterAssert(pxdata != NULL);

        CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
        CGContextRef context = CGBitmapContextCreate(pxdata, imgsize.width,
                                                     imgsize.height, 8, 4*imgsize.width, rgbColorSpace, 
                                                     kCGImageAlphaNoneSkipFirst);
        NSParameterAssert(context);
        CGContextConcatCTM(context, CGAffineTransformMakeRotation(0));
        CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image), 
                                               CGImageGetHeight(image)), image);
        CGColorSpaceRelease(rgbColorSpace);
        CGContextRelease(context);

        CVPixelBufferUnlockBaseAddress(pxbuffer, 0);

        appended = [pxlBufAdaptor appendPixelBuffer:pxbuffer withPresentationTime:presentationTime];
        CVBufferRelease(pxbuffer );
    }

问题是生成的视频包含损坏的图像 - 所有像素都是偏移的。看起来内存中填充了带有一些偏移量的字节,并且该偏移会破坏显示。

如何解决这个问题? 我想有任何胶水或方向。 提前谢谢。

1 个答案:

答案 0 :(得分:3)

这看起来很可疑:

CGContextRef context = CGBitmapContextCreate(pxdata, imgsize.width,
    imgsize.height, 8, 4*imgsize.width, rgbColorSpace, 
    kCGImageAlphaNoneSkipFirst);

您正在根据图像宽度计算bytesPerRow参数,而不是询问pxbuffer每行的字节数。试试这个:

CGContextRef context = CGBitmapContextCreate(pxdata, imgsize.width,
    imgsize.height, 8, CVPixelBufferGetBytesPerRow(pxbuffer),
    rgbColorSpace, kCGImageAlphaNoneSkipFirst);

此外,使用UIGraphicsGetCurrentContext创建位图图形上下文,将图层渲染到上下文中,从上下文中获取图像,破坏上下文,创建像素缓冲区,使用创建位图图形上下文似乎效率低下像素缓冲区,并将图层的图像绘制到新的上下文中。为什么不用CGContextDrawImage替换[self.imageSource.layer renderInContext:context]来电?