macos – 使用CGImageSourceRef加载图像时旋转错误

我使用下面的代码在C库中加载图像.加载某些图像时,图像的旋转是错误的.它似乎会影响来自iPhone相机的JPEG.我该如何解决?

据推测,有一个标志位于相机捕获的JPEG设置.以这种方式加载图像时我不确定如何访问它.

CGImageSourceRef source = CGImageSourceCreateWithURL(url, NULL);
CFRelease(url);

if(source==NULL)
    return IM_ErrFileError;

CGImageRef image = CGImageSourceCreateImageAtIndex(source, 0, NULL);
CFRelease(source);

if(image==NULL)
    return IM_ErrFileError;

int w = (int)CGImageGetWidth(image);
int h = (int)CGImageGetHeight(image);

im::Err err = img.Create(im::IntSize(w, h), bands, sizeof(uint8_t), imgtype);
if(IM_FAILED(err))
{
    CGImageRelease(image);
    return err;
}

img.Clear();

CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(colorspacename);
if(colorSpace==NULL)
{
    CGImageRelease(image);
    return IM_ErrAlloc;
}

// Create RGBA context
CGContextRef context = CGBitmapContextCreate(img.BytePtr(), w, h, 8, img.StrideBytes(), colorSpace, alphainfo);

if(context==NULL)
{
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(image);
    return IM_ErrAlloc;
}

CGColorSpaceRelease(colorSpace);

CGRect rect;

rect.origin.x = 0;
rect.origin.y = 0;
rect.size.width = w;
rect.size.height = h;

CGContextDrawImage(context, rect, image);

CGContextRelease(context);
CGImageRelease(image);

基于答案,这是我修改代码的方式.首先得到方向:

int orientation = 1;

CFDictionaryRef dict = CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
if(dict)
{
    CFNumberRef imageOrientation;

    if(CFDictionaryGetValueIfPresent(dict, kCGImagePropertyOrientation, (const void **)&imageOrientation))
    {
        if(imageOrientation)
            CFNumberGetValue(imageOrientation, kCFNumberIntType, &orientation);
    }

    CFRelease(dict);
}

确保尺寸正确:

int width = (int)CGImageGetWidth(image);
int height = (int)CGImageGetHeight(image);

int canvasw, canvash;

if(orientation<=4)
{
    canvasw = width;
    canvash = height;
}
else
{
    canvasw = height;
    canvash = width;
}

使用画布大小创建位图上下文图像.渲染时补偿方向:

switch(orientation)
{
    case 2:
        // 2 = 0th row is at the top, and 0th column is on the right - Flip Horizontal
        CGContextConcatCTM(context, CGAffineTransformMake(-1.0, 0.0, 0.0, 1.0, width, 0.0));
        break;

    case 3:
        // 3 = 0th row is at the bottom, and 0th column is on the right - Rotate 180 degrees
        CGContextConcatCTM(context, CGAffineTransformMake(-1.0, 0.0, 0.0, -1.0, width, height));
        break;

    case 4:
        // 4 = 0th row is at the bottom, and 0th column is on the left - Flip Vertical
        CGContextConcatCTM(context, CGAffineTransformMake(1.0, 0.0, 0, -1.0, 0.0, height));
        break;

    case 5:
        // 5 = 0th row is on the left, and 0th column is the top - Rotate -90 degrees and Flip Vertical
        CGContextConcatCTM(context, CGAffineTransformMake(0.0, -1.0, -1.0, 0.0, height, width));
        break;

    case 6:
        // 6 = 0th row is on the right, and 0th column is the top - Rotate 90 degrees
        CGContextConcatCTM(context, CGAffineTransformMake(0.0, -1.0, 1.0, 0.0, 0.0, width));
        break;

    case 7:
        // 7 = 0th row is on the right, and 0th column is the bottom - Rotate 90 degrees and Flip Vertical
        CGContextConcatCTM(context, CGAffineTransformMake(0.0, 1.0, 1.0, 0.0, 0.0, 0.0));
        break;

    case 8:
        // 8 = 0th row is on the left, and 0th column is the bottom - Rotate -90 degrees
        CGContextConcatCTM(context, CGAffineTransformMake(0.0, 1.0, -1.0, 0.0, height, 0.0));
        break;

    default:
        break;
}

最佳答案 我实际上没有尝试使用iPhone图像,但我建议使用CGImageSourceCopyPropertiesAtIndex来获取kCGImagePropertyOrientation.一旦知道了正确的方向,就可以在绘制时应用一些变换.

点赞