2009-12-21 88 views
3

我想添加陰影到UIImage視圖。我得到一個陰影,但它被剪切到圖像視圖的邊緣,我不知道爲什麼,因爲我正確地將uiimageview.clipsToBounds設置爲NO。下面是代碼:我想添加陰影到UImageView

-(void)addShadow 
{ 
    UIGraphicsBeginImageContext(self.frame.size); 
    CGContextRef myContext = UIGraphicsGetCurrentContext(); 
    float   myColorValues[] = {0, 0, 0, darkness};// 3 
    CGColorRef  myColor;// 4 
    CGColorSpaceRef myColorSpace; 
    CGContextSaveGState(myContext);// 6 

    myColorSpace = CGColorSpaceCreateDeviceRGB();// 9 
    myColor = CGColorCreate (myColorSpace, myColorValues);// 10 
    CGContextSetShadowWithColor (myContext, myShadowOffset, spread, myColor);// 11 
    // Your drawing code here// 12 
    // CGContextDrawImage(myContext, rotatingView.frame,imgRef); 

    rotatingView.clipsToBounds = NO; 
    [rotatingView.image drawInRect:rotatingView.frame 
         blendMode:kCGBlendModeNormal alpha:.5]; 
    CGColorRelease (myColor);// 13 
    CGColorSpaceRelease (myColorSpace); // 14 

    UIImage *imageCopy = UIGraphicsGetImageFromCurrentImageContext(); 
    CGContextRestoreGState(myContext); 
    UIGraphicsEndImageContext(); 
    rotatingView.image = imageCopy; 
} 

回答

2

我相信你傳遞的CGContextRef也有剪輯集,以防止基本上這種確切的行爲。您可能想嘗試添加一個CALayer:

CALayer       *layer = [CALayer layer]; 
CGRect       bounds = self.bounds; 

layer.bounds = bounds; 
layer.position = CGPointMake(bounds.size.width/2 + 5, bounds.size.height/2 + 5); 
layer.backgroundColor = [UIColor colorWithWhite: 0.10 alpha: 0.75].CGColor; 
layer.zPosition = -5; 

[self.layer addSublayer: layer]; 
+0

我試着添加這段代碼,但我仍然以修剪過的陰影結束。 – April 2009-12-21 23:40:29

+0

我是否在添加上面的代碼時做了其他錯誤?或者該圖層是否是影子? – April 2009-12-22 01:14:43

+0

該圖層應該是陰影。你確實將clipsToBounds設置爲NO? – 2009-12-22 01:26:23