如何向 UIImage 或 UIImageView 或 UIView 添加外发光

发布于 2024-08-21 19:27:07 字数 161 浏览 5 评论 0原文

我想向 UIImage/UIImageView/UIView 添加褪色阴影/外发光,但我不知道 Core Graphics 完全没有。

编辑: 请帮助!

I want to add a FADED shadow/outer glow to a UIImage/UIImageView/UIView but I know no Core Graphics at all.

Edit:
Please Help!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦罢 2024-08-28 19:27:07

采用 Cirrostratus 概述的方法,保留其缓存副本,然后在拖动时应用变换来更改图像的大小和/或位置。

(警告,这不是功能/测试代码,但应该让您开始)

-(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;
}

然后在您的方法中,在缩放时您会执行以下操作:

// 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.

一定要看看苹果文档。一个好的起点是 Quartz 2D 编程指南< /a>.

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.

(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.

夏日浅笑〃 2024-08-28 19:27:07

你可以使用这个,简单快捷,这可以使用 uiview、unbutton 等:

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;

如果你可以使用收音机,请添加以下内容:

shadowView.layer.shadowRadius = 10.0f;

You can use this, simple and fast, this works using uiview, unbutton, etc:

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;
女中豪杰 2024-08-28 19:27:07

出于性能考虑,iPhone OS 不支持通常可以在 Cocoa 中使用的shadowColor、shadowOffset、shadowOpacity 和shadowRadius 属性。大多数人会多次复制他们想要发光的形状,每次都会降低不透明度并每次偏移一个像素以模拟发光的外观。如果您的辉光不需要很大,您就无法分辨出差异。

As a performance consideration, iPhone OS does not support the shadowColor, shadowOffset, shadowOpacity, and shadowRadius properties, which you could normally use in 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文