簡體   English   中英

CGBitmapInfo上從Objective-C到Swift的轉換問題

[英]Objective-C to Swift Conversion Issue on CGBitmapInfo

我有這個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))

編輯**這是錯誤的樣子: 在此處輸入圖片說明

我相信以下是原始代碼的正確端口:

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開始(我相信),您必須對CGBitmapContextCreate的最后一個參數使用rawValue:

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)

您嘗試使用的類函數無效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM