Failed to flip CGImageRef on mac
===

I want to flip a CGImageRef.  1.Get the CGImageRef(myImg) I want to flip  2.Create a new bitmap context  3.flip the coordinate of the bitmap context 4.draw myImg to the bitmap context 5.Restore the bitmap context graphics state 6.Create image from the context
    

    CGImageRef myImg = MyCreateCGImageFromFile([NSString stringWithUTF8String:szImgPath]);
    // myImg is what I want to flip

    if (myImg != NULL)
    {
        // Create a bitmap context
        size_t pixelsHeight = CGImageGetHeight(myImg);
        size_t pixelsWidth = CGImageGetWidth(myImg);
        size_t bitsPerComponent = 8;
        size_t componentPerPixel = 4;
        size_t bytsPerRow = componentPerPixel * pixelsWidth;
        CGColorSpaceRef csRgb = CGColorSpaceCreateDeviceRGB();
        void* bitmapData = malloc(bytsPerRow * pixelsHeight);
        CGContextRef cgContextRef = CGBitmapContextCreate(bitmapData, pixelsWidth, pixelsHeight, bitsPerComponent,bytsPerRow,csRgb,kCGImageAlphaNoneSkipLast);
        
        // flip the coordinate of bitmap context
        CGContextSaveGState(cgContextRef);
        CGFloat sx = -1.0;
        CGFloat sy = -1.0;
        CGContextScaleCTM(cgContextRef, sx, sy);

        // draw myImg to bitmap context
        CGContextDrawImage(cgContextRef, CGRectMake(-pixelsWidth, -pixelsHeight, pixelsWidth, pixelsHeight), myImg);
        
        CGContextRestoreGState(cgContextRef);
        
        CGImageRef flipImg = CGBitmapContextCreateImage(cgContextRef);
        
        NSString* path = [NSString stringWithUTF8String:"/Users/jackson/Downloads/pdf/2.jpg"];
        CGImageWriteToFile(flipImg, path);
        ...
    }

The flipImg is the same with myImg.It does not flip.I don't what's wrong with it.