CG影像记录失真 [英] CGImage recording distortion

查看:87
本文介绍了CG影像记录失真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编写了一个帮助器类来记录NSView并将其保存到QuickTime文件中。该视图可以很好地记录到QuickTime影片中,但是由于某些原因输出会偏斜。我班级的核心在下面,输出是这样:

I've written a helper class to record a NSView and save it to a QuickTime file. The view is recorded fine to a QuickTime movie but the output is skewed for some reason. The core of my class is below, and the output is this:

- (void) captureImage
{
    [self getCGImageFromView];
    pixelBuffer = [self getPixelBufferFromCGImage:viewCGImage size:CGRectMake(0, 0, mViewRect.size.width, mViewRect.size.height).size];

    if(pixelBuffer) {
        if(![adapter appendPixelBuffer:pixelBuffer withPresentationTime:CMTimeMake(mCurrentFrame, 20)])
            NSLog(@"AVAssetWriterInputPixelBufferAdaptor: Failed to append pixel buffer.");

        CFRelease(pixelBuffer);
        mCurrentFrame++;
    }
}

- (void) getCGImageFromView
{
    viewBitmapImageRep = [currentView bitmapImageRepForCachingDisplayInRect:mViewRect];
    [currentView cacheDisplayInRect:mViewRect toBitmapImageRep:viewBitmapImageRep];

    viewBitmapFormat = [viewBitmapImageRep bitmapFormat];
    viewCGImage = [viewBitmapImageRep CGImage];
}

- (CVPixelBufferRef)getPixelBufferFromCGImage:(CGImageRef)image size:(CGSize)size
{
    NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
                        [NSNumber numberWithBool:YES], kCVPixelBufferCGImageCompatibilityKey,
                        [NSNumber numberWithBool:YES], kCVPixelBufferCGBitmapContextCompatibilityKey,
                        [NSNumber numberWithBool:YES], kCVPixelBufferOpenGLCompatibilityKey,
                        [NSNumber numberWithInt:size.width], kCVPixelBufferWidthKey,
                        [NSNumber numberWithInt:size.height], kCVPixelBufferHeightKey,
                         nil];

    CVPixelBufferRef pxbuffer = NULL;
    CVReturn status = CVPixelBufferCreate(NULL, size.width, size.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, size.width, size.height, 8, 4*size.width, rgbColorSpace, kCGImageAlphaPremultipliedFirst);
    NSParameterAssert(context);

    [[currentView layer] renderInContext:context];

    //CGContextDrawImage(context, CGRectMake(0, 0, CGImageGetWidth(image),    CGImageGetHeight(image)), image);
    CGColorSpaceRelease(rgbColorSpace);
    CGContextRelease(context);
    CVPixelBufferUnlockBaseAddress(pxbuffer, 0);

    return pxbuffer;
}


推荐答案

问题是这个

 4*size.width, 

size.width是一个奇数,不是4的倍数。传递给CGBitmapContextCreate的值必须是4的倍数。

The size.width was an odd number, not a multiple of 4. The values passed into CGBitmapContextCreate need to be a multiple of 4.

4*ceil(size.width / 4) * 4

解决了问题。

这篇关于CG影像记录失真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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