简体   繁体   English

如何向UIImage或UIImageView或UIView添加外部光晕

[英]How do I add an outer glow to a UIImage or UIImageView or UIView

I want to add a FADED shadow/outer glow to a UIImage / UIImageView / UIView but I know no Core Graphics at all. 我想为UIImage / UIImageView / UIView添加一个FADED阴影/外部光晕但我根本不知道Core Graphics

Edit: Please Help!! 编辑:请帮助!!

Take the approach outlined by Cirrostratus, keep a cached copy of it, and then apply a transform to change the size and/or position of the image while dragging. 采用Cirrostratus概述的方法,保留其缓存副本,然后应用变换以在拖动时更改图像的大小和/或位置。

(warning, this is not functional/tested code, but should get you started) (警告,这不是功能/测试过的代码,但应该让你入门)

-(UIImage*)addGlowToImage:(UIImage*)imageInput;
{
    CGRect newSize = imageInput.bounds;
    CGImageRef theImage = imageInput.CGImage;

    // expand the size to handle the "glow"
    newSize.size.width += 6.0;
    newSize.size.height += 6.0;
    UIGraphicsBeginImageContext(newSize);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    CGContextBeginTransparencyLayerWithRect(ctx, newSize, NULL);
    CGContextClearRect(ctx, newSize);

    // you can repeat this process to build glow.
    CGContextDrawImage(ctx, newSize, theImage); 
    CGContextSetAlpha(ctx, 0.2);  

    CGContextEndTransparencyLayer(ctx);

    // draw the original image into the context, offset to be centered;
    CGRect centerRect = inputImage.bounds;
    centerRect.origin.x += 3.0;
    centerRect.origin.y += 3.0;
    CGContextDrawImage(ctx, centerRect, theImage);

    result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return result;
}

Then in your method while scaling you would do something like: 然后在你的方法缩放时你会做类似的事情:

// assumes UIImage *cachedImage = [self addGlowToImage:origImage]; has been called already.
// assumes ivars for scale exists

    CGRect newRect = cachedImage.bounds;
    newRect.size.width += scale;
    newRect.size.height += scale;

    [cachedImage drawInRect:newRect];  // image will be scaled to fill destination rectangle.

Definitely take a look at the apple docs. 绝对看看苹果文档。 A good starting place is the Quartz 2D Programming Guide . 一个很好的起点是Quartz 2D Programming Guide

You can use this, simple and fast, this works using uiview, unbutton, etc: 您可以使用这个,简单快速,使用uiview,解锁等工作:

UIView *shadowView = [[UIView alloc] initWithFrame:CGRectMake(100, 66, 112, 112)];
shadowView.layer.shadowColor = [[UIColor blackColor] CGColor];
shadowView.layer.shadowOpacity = .4;
shadowView.layer.shadowOffset = CGSizeZero;
shadowView.layer.masksToBounds = NO;

if, you can use radios, add this: 如果,你可以使用无线电,添加这个:

shadowView.layer.shadowRadius = 10.0f;

As a performance consideration, iPhone OS does not support the shadowColor, shadowOffset, shadowOpacity, and shadowRadius properties, which you could normally use in Cocoa. 作为性能考虑因素,iPhone OS不支持shadowColor,shadowOffset,shadowOpacity和shadowRadius属性,这些属性通常可以在Cocoa中使用。 Most people duplicate the shape they want to glow several times with each time lowering the opacity and offsetting the shape one pixel at a time to simulate the look of a glow. 大多数人复制他们想要发光几次的形状,每次降低不透明度并一次偏移一个像素的形状以模拟发光的外观。 If your glow does not need to be very big, you cannot tell the difference. 如果你的发光不需要很大,你就无法区分它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM