2016-05-04 14 views
0

このObjective-Cコードは、フィルタの不透明な背景を取り除きます。私はそれを最新のSwiftに変換しようとしており、どこにでもエラーがあります。Objective-C to Swift Conversion CGBitmapInfoの問題

-(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; 
} 

1行ずつ変換します。私はこれまで、このように得ている、まだラインを変換する問題がある:私は次のように元のコードの正しいポートであると考えてい enter image description here

+0

エラーは何ですか?私には( 'CGBitmapInfo'の)' rawValue: 'initialiser引数がないようです。また、 'context'は' let'定数でなければなりません。 – Hamish

+0

オリジナルの投稿を更新しました。 – Gizmodo

+0

なぜスウィフトコードに変換していますか?なぜ迅速にブリッジを使用して客観的なCコードを使用していないのですか? https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html – Hasya

答えて

0

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

EDIT ** する。これは、どのようにエラーが見ています:ここ

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 
} 

は一例であり、(オリジナル - >出力):

originalスウィフト2(私は信じている)ので、変更

を説明する

removeColorFromImage(original, grayLevel: 175) 

、あなたは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) 

クラスの機能ではありません有効です。