- (UIImage *)roundedCornerImage:(NSInteger)cornerSize borderSize:(NSInteger)borderSize {
// If the image does not have an alpha layer, add one
UIImage *image = [self imageWithAlpha];
// Build a context that's the same dimensions as the new size
CGBitmapInfo info = CGImageGetBitmapInfo(image.CGImage);
CGContextRef context = CGBitmapContextCreate(NULL,
image.size.width,
image.size.height,
CGImageGetBitsPerComponent(image.CGImage),
0,
CGImageGetColorSpace(image.CGImage),
CGImageGetBitmapInfo(image.CGImage));
// Create a clipping path with rounded corners
CGContextBeginPath(context);
[self addRoundedRectToPath:CGRectMake(borderSize, borderSize, image.size.width - borderSize * 2, image.size.height - borderSize * 2)
context:context
ovalWidth:cornerSize
ovalHeight:cornerSize];
CGContextClosePath(context);
CGContextClip(context);
// Draw the image to the context; the clipping path will make anything outside the rounded rect transparent
CGContextDrawImage(context, CGRectMake(0, 0, image.size.width, image.size.height), image.CGImage);
// Create a CGImage from the context
CGImageRef clippedImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
// Create a UIImage from the CGImage
UIImage *roundedImage = [UIImage imageWithCGImage:clippedImage];
CGImageRelease(clippedImage);
return roundedImage;
}
Iは、上記の方法を有しており、Twitterのプロフィール画像に丸い角を添加しています。ほとんどの画像について、これはすごく効果的です。次のエラーが発生する原因となるいくつかがあります<Error>:CGBitmapContextCreate:サポートされていないパラメータの組み合わせ対低解像度画像
:CGBitmapContextCreate:サポートされていないパラメータの組み合わせ:8整数ビット/コンポーネント。 32ビット/ピクセル; 3成分色空間; kCGImageAlphaLast; 96バイト/行。
私はいくつかのデバッグを行っているとコンテキストを作成するときに、エラーとそうでないものを引き起こし画像から唯一の違いは、CGImageGetBitmapInfo(image.CGImage)のパラメータであるように見えます。これにより、エラーがスローされ、コンテキストがnullになります。最後のパラメータをkCGImageAlphaPremultipliedLastに設定してみました。今度は画像が描画されますが、品質ははるかに低くなります。残りの部分と同じように高画質の画像を得る方法はありますか?画像へのパスはTwitterを経由しているので、あなたが引き出すことができるものが異なるかどうかはわかりません。
このエラーに関するその他の質問もあります。いずれもこの問題を解決していません。私はthis postを見ましたが、その後にエラーが発生した画像は完全にぼやけています。幅と高さをNSIntegerにキャストすることもうまくいかなかった。以下は、2つのプロファイル画像のスクリーンショットとその品質です。最初のエラーが原因です。
誰もが問題はここにあるものの任意のアイデアを持っていますか?
ありがとうございます。これが私を殺している。
'imageWithAlpha'によって返された元の画像は、1.0以外の' scale'値を持っていますか? –
ぼやけた画像、またはエラーのある画像の尺度値は2.0で、その他の尺度値は1.0です。うまくいけば、これは役に立ちます... – kschins