IOS 5: UIView converted to video and the resulting

2019-07-28 05:19发布

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条回答
太酷不给撩
2楼-- · 2019-07-28 05:33

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

查看更多
登录 后发表回答