2012-02-29 3 views
1
- (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つのプロファイル画像のスクリーンショットとその品質です。最初のエラーが原因です。

Here is a screenshot of the two images and their quality

誰もが問題はここにあるものの任意のアイデアを持っていますか?

ありがとうございます。これが私を殺している。

+0

'imageWithAlpha'によって返された元の画像は、1.0以外の' scale'値を持っていますか? –

+0

ぼやけた画像、またはエラーのある画像の尺度値は2.0で、その他の尺度値は1.0です。うまくいけば、これは役に立ちます... – kschins

答えて

4

iOSはkCGImageAlphaLastをサポートしていません。 kCGImageAlphaPremultipliedLastを使用する必要があります。

初期画像の縮尺も処理する必要があります。あなたの現在のコードはそうではないので、スケールが2.0の場合、イメージをダウンサンプリングします。

UIKit関数とクラスを使用すると、関数全体を簡単に記述できます。 UIKitはあなたのためにスケールを処理します。グラフィックスコンテキストを作成するように頼んだら元の画像のスケールを渡すだけです。あなたのimageWithAlpha方法自体は別のUIImageからUIImageを作成する場合

- (UIImage *)roundedCornerImage:(NSInteger)cornerSize borderSize:(NSInteger)borderSize { 
    // If the image does not have an alpha layer, add one 
    UIImage *image = [self imageWithAlpha]; 

    UIGraphicsBeginImageContextWithOptions(image.size, NO, image.scale); { 
     CGRect imageRect = (CGRect){ CGPointZero, image.size }; 
     CGRect borderRect = CGRectInset(imageRect, borderSize, borderSize); 
     UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:borderRect 
      byRoundingCorners:UIRectCornerAllCorners 
      cornerRadii:CGSizeMake(cornerSize, cornerSize)]; 
     [path addClip]; 
     [image drawAtPoint:CGPointZero]; 
    } 
    UIImage *roundedImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 
    return roundedImage; 
} 

が、それはまた、規模を伝播する必要があります。

+0

画像はまだぼやけています。私はTwitterから得ているイメージを他のサイズの半分のサイズにしています。より高い解像度、またはより大きな画像を得る方法はありますか?または、私はどのようにそれの品質を扱うことができますか? – kschins

+0

私は自分の答えを変更しました。 –

+0

地獄はい - どうもありがとうございます。 – kschins