34

迅速に CGContext を作成しようとしています。コンパイルはできますが、実行時にエラーがスローされます。

let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
let context:CGContext = CGBitmapContextCreate(nil, 20, 20, 8, 0, colorSpace, CGBitmapInfo.AlphaInfoMask)
CGColorSpaceRelease(colorSpace);

....

エラーは次のとおりです。

Error: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; unrecognized; 96 bytes/row.
fatal error: Can't unwrap Optional.None
4

8 に答える 8

5

Swift 5用に更新:

 let colorSpace:CGColorSpace = CGColorSpaceCreateDeviceRGB()
 let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.premultipliedLast.rawValue)
 let context = CGContext(data: nil, width: UInt(rect.size.width), height: UInt(rect.size.height), bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: bitmapInfo.rawValue)

rect には高さと幅があります

于 2019-12-11T07:50:40.150 に答える
1

Swift 2.2 では:

let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue).rawValue
let colorSpace = CGColorSpaceCreateDeviceRGB()
let context = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo)
于 2016-04-29T21:05:57.757 に答える
0

CGBitmapInfo.AlphaInfoMask は有効なビットマップ情報ではありません。

CGBitmapInfo.AlphaLast または CGBitmapInfo.AlphaFirst を設定してみてください。

于 2014-06-08T19:18:43.550 に答える