IOS 5: UIView converted to video and the resulting video is corrupted

三世轮回 提交于 2019-12-11 12:42:24

问题


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.


回答1:


This looks suspicious:

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

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

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]?



来源:https://stackoverflow.com/questions/8964695/ios-5-uiview-converted-to-video-and-the-resulting-video-is-corrupted

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!