【问题标题】:Objective-C to Swift Conversion Issue on CGBitmapInfoCGBitmapInfo 上的 Objective-C 到 Swift 转换问题
【发布时间】:2016-05-04 21:09:47
【问题描述】:

我有这个 Objective-C 代码,它可以去除过滤器的不透明背景。我正在尝试将其转换为最新的 Swift,并且到处都有错误。

-(UIImage*)removeColorFromImage:(UIImage*)sourceImage grayLevel:(int)grayLevel
{
    int width = sourceImage.size.width * sourceImage.scale;
    int height = sourceImage.size.height * sourceImage.scale;
    CGFloat scale = sourceImage.scale;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef context = CGBitmapContextCreate(NULL, width, height, 8, width * 4, colorSpace, kCGBitmapAlphaInfoMask & kCGImageAlphaPremultipliedFirst);
    CGColorSpaceRelease(colorSpace);

    CGContextDrawImage(context, CGRectMake(0, 0, width, height), sourceImage.CGImage);

    unsigned int *colorData = CGBitmapContextGetData(context);

    for (int i = 0; i < width * height; i++)
    {
        unsigned int color = *colorData;

        short a = color & 0xFF;
        short r = (color >> 8) & 0xFF;
        short g = (color >> 16) & 0xFF;
        short b = (color >> 24) & 0xFF;

        if ((r == grayLevel) && (g == grayLevel) && (b == grayLevel))
        {
            a = r = g = b = 0;
            *colorData = (unsigned int)(r << 8) + ((unsigned int)(g) << 16) + ((unsigned int)(b) << 24) + ((unsigned int)(a));
        }

        colorData++;
    }

    CGImageRef output = CGBitmapContextCreateImage(context);
    UIImage* retImage = [UIImage imageWithCGImage:output scale:scale orientation:UIImageOrientationUp];

    CGImageRelease(output);
    CGContextRelease(context);

    return retImage;
}

当我逐行转换时。我已经做到了这一点,但在转换行时遇到问题:

var context = CGBitmapContextCreate(nil, width, height, 8, width * 4, colorSpace, CGBitmapInfo(CGImageAlphaInfo.PremultipliedFirst.rawValue))

编辑** 这是错误的样子:

【问题讨论】:

标签: objective-c xcode swift cgimage cgbitmapcontext


【解决方案1】:

我相信以下是原始代码的正确移植:

func removeColorFromImage(sourceImage: UIImage, grayLevel: UInt32) -> UIImage? {
    let scale = sourceImage.scale
    let width = Int(sourceImage.size.width * scale)
    let height = Int(sourceImage.size.height * scale)
    let colorSpace = CGColorSpaceCreateDeviceRGB()
    let bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.AlphaInfoMask.rawValue & CGImageAlphaInfo.PremultipliedFirst.rawValue)
    let context = CGBitmapContextCreate(nil, width, height, 8, width * 4, colorSpace, bitmapInfo.rawValue)
    CGContextDrawImage(context, CGRectMake(0, 0, CGFloat(width), CGFloat(height)), sourceImage.CGImage)

    let uncastedData = CGBitmapContextGetData(context)
    let colorData = UnsafeMutablePointer<UInt32>(uncastedData)
    for i in 0..<width * height {
        let color = colorData[i]
        var a = color & 0xFF
        var r = (color >> 8) & 0xFF
        var g = (color >> 16) & 0xFF
        var b = (color >> 24) & 0xFF
        if ((r == grayLevel) && (g == grayLevel) && (b == grayLevel)) {
            (a, r, g, b) = (0, 0, 0, 0)
            colorData[i] = (r << 8) + (g << 16) + (b << 24) + a
        }
        colorData.advancedBy(1)
    }

    if let output = CGBitmapContextCreateImage(context) {
        return UIImage(CGImage: output, scale: scale, orientation: UIImageOrientation.Up)
    }

    return nil
}

这是一个示例(原始 -> 输出):

removeColorFromImage(original, grayLevel: 175)

解释变化

从 Swift 2 开始(我相信),你必须使用 rawValue 作为 CGBitmapContextCreate 的最后一个参数:

let context = CGBitmapContextCreate(nil, width, height, 8, width * 4, colorSpace, bitmapInfo.rawValue)

另外,在您的移植中,您没有使用来自 Objective-C 代码的原始值:

// your new code was just using CGImageAlphaInfo.PremultipliedFirst.rawValue
// to match, it should be:
let bitmapInfo = CGBitmapInfo(rawValue: CGBitmapInfo.AlphaInfoMask.rawValue & CGImageAlphaInfo.PremultipliedFirst.rawValue)

您在 C 风格的 for 循环中进行的位移是针对无效类型的。它现在在 UInt32s 上。我也为你摆脱了 C 风格的循环警告 :)

最后,你需要使用这个来初始化最终确定的 UIImage:

UIImage(CGImage: output, scale: scale, orientation: UIImageOrientation.Up)

您尝试使用的类函数无效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-01
    • 2016-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 1970-01-01
    相关资源
    最近更新 更多