CGBitmapContextCreate: unsupported parameter combinationというエラーが出る

Objective-Cで、CGRect型のmyframeのサイズの画像を生成しようとするとエラーが起きた。

CGContextRef context = CGBitmapContextCreate(NULL, myframe.size.width, myframe.size.height,
                                             8, myframe.size.width * 4,
                                             colorSpace, kCGImageAlphaPremultipliedFirst);

起きたエラーの内容は以下のとおり。

<Error>: CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaPremultipliedFirst; 806 bytes/row.

原因は、CGRect型のwidthやheightはCGFloat型なので、”myframe.size.width * 4″の箇所であるべき数値と異なる数値が計算されてしまっているようだった。
なので、このようにコードを書き換えたら解決した。

int width = myframe.size.width;
int height = myframe.size.height;
CGContextRef context = CGBitmapContextCreate(NULL, width, height,
                                             8, width * 4,
                                             colorSpace, kCGImageAlphaPremultipliedFirst);

本来はfloor()とかで数値を切り捨てるんだろうけど、とりあえずはこれで動いた。

Pocket
LINEで送る
LinkedIn にシェア