如何在 iOS UIButton 中添加阴影?

How to add Drop Shadow in iOS UIButton?

需要专家的帮助

我正在自定义 UIButton,我正在裁剪背景并在 UIButton 上显示,如下所示。

[self setBackgroundImage:[self getSingleColorImageForLinear:self.frame] forState:UIControlStateSelected];




 //Method to make take image
-(UIImage *)getSingleColorImageForLinear:(CGRect)frame{


    CGSize size = CGSizeMake(frame.size.width,frame.size.height);
    UIGraphicsBeginImageContextWithOptions(size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();

    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();

    size_t gradientNumberOfLocations = 1;
    CGFloat gradientLocations[1] = { 0.0 };
    CGFloat gradientComponents[4] = { 0, 0, 0, 0.3, };

    CGGradientRef gradient = CGGradientCreateWithColorComponents (colorspace, gradientComponents, gradientLocations, gradientNumberOfLocations);

    CGContextDrawLinearGradient(context, gradient, CGPointMake(0, 0), CGPointMake(0, size.height), 0);


    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    CGGradientRelease(gradient);
    CGColorSpaceRelease(colorspace);
    UIGraphicsEndImageContext();

    return image;

}

现在我想将在 Adob​​e Photoshop 中看到的 属性 投影应用于此图像,

如何使用 Core Graphics 或 iOS 库的任何 Core Foundation 来做到这一点?

Adobe 的投影如下所示

谢谢

button.layer.shadowColor = [UIColor blackColor].CGColor;
button.layer.shadowOffset = CGSizeMake(5, 5);
button.layer.shadowRadius = 5;
button.layer.shadowOpacity = 0.5;