2011-01-08 18 views
2

コアグラフィックスを使用してイメージを描画しようとしています。ここに私のコードの抜粋です:丸みを帯びたコーナーと影を持つココア描画イメージ

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

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

ありがとうございます!

答えて

11

わかりましたので、あなたはUIImageであるインスタンス変数、画像を、持っているの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はその下を指します。希望が役立ちます。あなたは層でImageViewのを使用することができます

0

、層が影との境界線を設定するためのプロパティを持っているか、これがされています。このことができます

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が

を上書きします使用して、次に描画するはるかに高速です
2

Daniel Thorpeの答えを使用して画像の角を丸める関数です。ここに来た場合、私のように、これを行う方法を探しています。

+ (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; 
} 
関連する問題