【问题标题】:How do I add an outer glow to a UIImage or UIImageView or UIView如何向 UIImage 或 UIImageView 或 UIView 添加外发光
【发布时间】:2010-02-17 05:02:31
【问题描述】:

我想为UIImage/UIImageView/UIView 添加 FADED 阴影/外发光,但我根本不知道 Core Graphics

编辑: 请帮忙!!

【问题讨论】:

    标签: iphone cocoa-touch uiimageview uiimage core-graphics


    【解决方案1】:

    采用 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 Programming Guide

    【讨论】:

    • 好吧,不完全是我想要的,但我会接受,我想要褪色。
    • 如果你改变颜色,你也可以使用 shadow 属性作为发光。简单易行。
    • 2010年,给出答案的时候,是合适的。如果您不喜欢这种方法,请使用一些可用的新 api 提供更好的答案。
    • @MarkMolina 我同意 Chip Coons 的观点。如果您不喜欢这种方法,请自己提供一个更好的答案,而不是批评其他人的答案。一点用也没有。
    • 当然有帮助。它警告毫无戒心的程序员糟糕的代码,也许他们可以改进该代码。我不想唠叨代码。人们在这些主题上投入时间真是太好了,但我只是说代码不是最优的
    【解决方案2】:

    你可以使用这个,简单快速,使用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;
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 如果视图每秒更新多次,这将占用大量性能。我正在使用touchesMoved 事件更改大小。
      猜你喜欢
      • 2013-08-26
      • 2011-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多