2

コア グラフィックスを使用して、角を丸くし、ドロップ シャドウを付けて画像を描画しようとしています。ここに私のコードのスニペットがあります:

CGContextSetShadowWithColor(context, CGSizeMake(0, 1), 2, shadowColor);    
CGContextAddPath(context, path);
CGContextClip(context);
CGContextDrawImage(context, rect, image);

私が抱えている問題は、丸みを帯びた角を作成するためのクリッピングが影もクリッピングしていることです。画像は部分的に透明になる可能性があるため、画像の下に影を付けて角丸長方形を単純に描画することはできません。最初に画像に丸みを帯びた形状を適用してから、結果の画像を画面に描画して影を追加する必要があると思います。誰もこれを行う方法を知っていますか?

ありがとう!

4

3 に答える 3

11

さて、UIImage であるインスタンス変数 image を持つ UIView サブクラスがあると仮定すると、次のように drawRect: 関数を実行できます...

- (void)drawRect:(CGRect)rect {
    [super drawRect:rect];

    CGRect _bounds = [self bounds];
    CGColorRef aColor;
    CGContextRef context = UIGraphicsGetCurrentContext();

    // Create a path
    CGRect insetRect = CGRectInset(_bounds, kBSImageButtonBorder, kBSImageButtonBorder);
    CGRect offsetRect = insetRect; offsetRect.origin = CGPointZero;

    UIGraphicsBeginImageContext(insetRect.size);
    CGContextRef imgContext = UIGraphicsGetCurrentContext();
    CGPathRef clippingPath = [UIBezierPath bezierPathWithRoundedRect:offsetRect cornerRadius:CORNER_RADIUS].CGPath; 
    CGContextAddPath(imgContext, clippingPath);
    CGContextClip(imgContext);  
    // Draw the image
    [image drawInRect:offsetRect];
    // Get the image
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    // Setup the shadow
    aColor = [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.5f].CGColor;
    CGContextSetShadowWithColor(context, CGSizeMake(0.0f, 2.0f), 2.0f, aColor);     

    // Draw the clipped image in the context
    [img drawInRect:insetRect];

}

私は自分自身でQuartzプログラミングに少し慣れていませんが、それはあなたのイメージを長方形の中心に置き、境界線を引いたもので、角の半径と2.fポイントの影がその下に2.fポイントあるはずです。それが役立つことを願っています。

于 2011-04-13T17:36:26.400 に答える
3

私のように、これを行う方法を探しているだけでここに来た場合に備えて、ダニエル・ソープの答えを使用して画像の角を丸める関数を次に示します。

+ (UIImage *) roundCornersOfImage:(UIImage *)image toRadius:(float)radius {

    // create image sized context
    UIGraphicsBeginImageContext(image.size);
    CGContextRef context = UIGraphicsGetCurrentContext();

    // add rounded rect clipping path to context using radius
    CGRect imageBounds = CGRectMake(0, 0, image.size.width, image.size.height);
    CGPathRef clippingPath = [UIBezierPath bezierPathWithRoundedRect:imageBounds cornerRadius:radius].CGPath;
    CGContextAddPath(context, clippingPath);
    CGContextClip(context);

    // draw the image
    [image drawInRect:imageBounds];

    // get the image
    UIImage *outImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return outImage;
}
于 2014-10-09T17:31:11.970 に答える
0

レイヤーでimageViewを使用できます。レイヤーには、影と境界線を設定するためのプロパティがあります。これは次のとおりです。

    self.imageView = [[NSImageView alloc] initWithFrame:NSMakeRect(0,0,60,60)];
    self.imageView.image = [NSImage imageNamed:@"yourImageName"];
    self.imageView.wantsLayer = YES;
    self.imageView.layer.cornerRadius = 10;
    //make the shadow and set it
    NSShadow* shadow = [[NSShadow alloc] init];
    shadow.shadowBlurRadius = 2;
    shadow.shadowOffset = NSMakeSize(2, -2);
    shadow.shadowColor = [NSColor blackColor];
    self.imageView.shadow = shadow;

これが役立つことを願っています。これは、drawRect オーバーライドを使用するよりも描画がはるかに高速です。

于 2013-03-11T17:54:36.543 に答える