0

フィルターの不透明な背景を取り出すこの 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))

編集**これはエラーがどのように見えるかです: ここに画像の説明を入力

4

1 に答える 1