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

标签 iphone cocoa-touch uiimageview uiimage core-graphics

我想向 UIImage 添加褪色阴影/外发光/UIImageView/UIView但我不知道Core Graphics根本不。

编辑: 请帮忙!!

最佳答案

采用 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 .

关于iphone - 如何向 UIImage 或 UIImageView 或 UIView 添加外部发光,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2278440/

相关文章:

ios - 为什么图像拉伸(stretch)(UIImage 到 CIImage 到 UIImage)

ios - swift 2 将 int 转换为 numberFromString 作为 String

ios - 使用 XCode 4.5.1 分发时出错

iphone - CoreData中通过NSPredicate获取某个属性的所有值

iphone - 通过查看相邻页面来分页 UIScrollView

ios - UIImageView 图片旋转

ios - iOS sdk 中图像的纵横比问题

c# - 如何在 C# 中创建自定义 JSON 以及返回值应该是什么?

iphone - 如何知道 dismissModalViewControllerAnimated 何时启动以及何时完成?

iphone - 为 iPhone 开发人员学习 Mac OS X 开发的好资源