2

我正在使用以下代码(从其他地方收集)标准化图像的方向和最大尺寸:

+ (UIImage *)scaleAndRotateImage:(UIImage *)image
{
    int kMaxResolution = 960; // Or whatever

    CGImageRef imgRef = image.CGImage;

    CGFloat width = CGImageGetWidth(imgRef);
    CGFloat height = CGImageGetHeight(imgRef);

    CGAffineTransform transform = CGAffineTransformIdentity;
    CGRect bounds = CGRectMake(0, 0, width, height);
    if (width > kMaxResolution || height > kMaxResolution) {
        CGFloat ratio = width/height;
        if (ratio > 1) {
            bounds.size.width = kMaxResolution;
            bounds.size.height = bounds.size.width / ratio;
        }
        else {
            bounds.size.height = kMaxResolution;
            bounds.size.width = bounds.size.height * ratio;
        }
    }

    CGFloat scaleRatio = bounds.size.width / width;
    CGSize imageSize = CGSizeMake(CGImageGetWidth(imgRef), CGImageGetHeight(imgRef));
    CGFloat boundHeight;
    UIImageOrientation orient = image.imageOrientation;
    switch(orient) {

        case UIImageOrientationUp: //EXIF = 1
            transform = CGAffineTransformIdentity;
            break;

        case UIImageOrientationUpMirrored: //EXIF = 2
            transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            break;

        case UIImageOrientationDown: //EXIF = 3
            transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;

        case UIImageOrientationDownMirrored: //EXIF = 4
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.height);
            transform = CGAffineTransformScale(transform, 1.0, -1.0);
            break;

        case UIImageOrientationLeftMirrored: //EXIF = 5
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width);
            transform = CGAffineTransformScale(transform, -1.0, 1.0);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;

        case UIImageOrientationLeft: //EXIF = 6
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(0.0, imageSize.width);
            transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0);
            break;

        case UIImageOrientationRightMirrored: //EXIF = 7
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeScale(-1.0, 1.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;

        case UIImageOrientationRight: //EXIF = 8
            boundHeight = bounds.size.height;
            bounds.size.height = bounds.size.width;
            bounds.size.width = boundHeight;
            transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0);
            transform = CGAffineTransformRotate(transform, M_PI / 2.0);
            break;

        default:
            [NSException raise:NSInternalInconsistencyException format:@"Invalid image orientation"];

    }

    UIGraphicsBeginImageContext(bounds.size);

    CGContextRef context = UIGraphicsGetCurrentContext();

    if (orient == UIImageOrientationRight || orient == UIImageOrientationLeft) {
        CGContextScaleCTM(context, -scaleRatio, scaleRatio);
        CGContextTranslateCTM(context, -height, 0);
    }
    else {
        CGContextScaleCTM(context, scaleRatio, -scaleRatio);
        CGContextTranslateCTM(context, 0, -height);
    }

    CGContextConcatCTM(context, transform);

    CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, width, height), imgRef);
    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();


    return imageCopy;

}

此代码正确返回一个 UIImage,其旋转转换为 UIImageOrientationUp,如果超过最大图像大小,图像可能会按比例缩小。此代码适用于在 UIView 中显示 UIImage。

但是,如果我采用新的 UIImage 并将其 CGImage 内容提取到位图上下文中,则会在下一步出现我的问题。

CGImageRef inImage = inputImage.CGImage;

// create data object for direct pixel manipulation
CFDataRef m_DataRef;
    m_DataRef = CGDataProviderCopyData(CGImageGetDataProvider(inImage));
    UInt8 * m_PixelBuf = (UInt8 *) CFDataGetBytePtr(m_DataRef);

//create initial context for drawing on the eyelids
CGContextRef ctx = CGBitmapContextCreate(m_PixelBuf,
                                         CGImageGetWidth( inImage ),
                                         CGImageGetHeight( inImage ),
                                         8,
                                         CGImageGetBytesPerRow( inImage ),
                                         CGImageGetColorSpace( inImage ),
                                         CGImageGetAlphaInfo( inImage ) );

然后将其转换回 UIImage 所有颜色都是错误的。我检查了从 inImage 获取的 CGImageGetAlphaInfo 以设置设置为 kCGImageAlphaPremultipliedFirst 的上下文,如果我强制 AlphaInfo 为 kCGImageAlphaPremultipliedLast 我至少可以看到图像,但红色显然显示为蓝色。最后,我发现位图中的 RGBA 数据已转置为 ABGR,通过创建一个循环将字节顺序转回 RGBA,我发现我可以再次使用从 CGImage 创建的上下文重新创建正确的 UIImage。

只有当我通过缩放和旋转方法时,我才会遇到这个 ABGR 问题。我的理解是,我应该能够将 UIImage 转换为 CGbitmapContext 并再次使用它自己的元数据作为上下文参数来接收完全相同的图像。

我的问题是:为什么位图像素字节顺序更改为 ABGR?CGImage 元数据如何变得混乱,以至于我无法直接从其自身创建的 BitmapContext 重新创建 CGImage?

提前致谢!

4

0 回答 0