Problems with writting CGImageRef to CVPixelBufferRef on iPhone XS

Hey fellow developers!


Just got my iPhone Xs, I'm super excited!!! However, there's something on my mind! My code doesn't work anymore without any changes.


- [✅] iPhone 6

- [✅] iPhone 8

- [╳] iPhone XS


The flow as the following, I created an `AVAssetWriterInputPixelBufferAdaptor` with the following options.


    NSDictionary *options = @{(NSString *)kCVPixelBufferWidthKey: @(size.width),
                               (NSString *)kCVPixelBufferHeightKey: @(size.height),
                               (NSString *)kCVPixelBufferCGImageCompatibilityKey: @(YES),
                               (NSString *)kCVPixelBufferCGBitmapContextCompatibilityKey: @(YES),
                               (NSString *)kCVPixelBufferPixelFormatTypeKey: @(kCVPixelFormatType_32BGRA)
                               };

And write to the CVPixelBufferRef like this

- (void) writeFrameAt: (NSInteger) frameIndex
             forFrame: (UIImage *) frame
                atFPS: (int) fps
            toApaptor: (AVAssetWriterInputPixelBufferAdaptor *) adp {
    
    CGImageRef image = [frame CGImage];

    CVPixelBufferRef pxbuffer = NULL;
    CVPixelBufferPoolCreatePixelBuffer(NULL, adp.pixelBufferPool, &pxbuffer);
    
    CVPixelBufferLockBaseAddress(pxbuffer, 0);
    void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
    NSParameterAssert(pxdata != NULL);
    
    CGFloat height = CGImageGetHeight(image);
    CGFloat width = CGImageGetWidth(image);
    CGFloat bytesPerRow = CGImageGetBytesPerRow(image);
    CGFloat bPerComponent = CGImageGetBitsPerComponent(image);
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo info = CGImageGetBitmapInfo(image);
    
    CGContextRef context = CGBitmapContextCreate(pxdata, width, height, bPerComponent, bytesPerRow, colorSpace, info);
    NSParameterAssert(context);
    
    CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
    
    CGColorSpaceRelease(colorSpace);
    CGContextRelease(context);
    CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
    
    BOOL result = [adp appendPixelBuffer:pxbuffer withPresentationTime:CMTimeMake(frameIndex, fps)];
    NSParameterAssert(result);
    
    CFRelease(pxbuffer);
}


Video from iPhone 6/8 looks like this:https://i.imgur.com/RE3XwO0.png

Video from iPhone Xs looks like this: https://i.imgur.com/ICA5I9p.jpg



Anyone can shine some light on this problem?



Thanks!!!