IOS 5:UIView转换为视频,结果视频已损坏 [英] IOS 5: UIView converted to video and the resulting video is corrupted

查看:190
本文介绍了IOS 5:UIView转换为视频,结果视频已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to grab the UIView, convert to image and than store that image in a video file (.mp4). I use the next portion of code which grabs the image and puts it to pixel buffer:

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 );
    }

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

The problem is that the resulting video contains the corrupted image - all pixels are offset . It looks like the memory is filled with bytes with some offset and that offset corrupts the presentation.

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

How can this be fixed? I would like to have any glue or direction. Thanks in advance.

推荐答案

这看起来很可疑:

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

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

You are computing the bytesPerRow parameter based on the image width, instead of asking pxbuffer for its bytes-per-row. Try this:

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

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

Also, it seems inefficient to create a bitmap graphics context with UIGraphicsGetCurrentContext, render the layer into the context, get an image from the context, destroy the context, create a pixel buffer, create a bitmap graphics context using the pixel buffer, and draw the image of the layer into the new context. Why not replace your CGContextDrawImage call with [self.imageSource.layer renderInContext:context]?

这篇关于IOS 5:UIView转换为视频,结果视频已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆