如何向UIImage或UIImageView或UIView添加外部光晕 [英] How do I add an outer glow to a UIImage or UIImageView or UIView

查看:527
本文介绍了如何向UIImage或UIImageView或UIView添加外部光晕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 UIImage / 中添加FADED阴影/外部光晕UIImageView / UIView 但我知道没有核心图形



编辑
请帮助!!

解决方案

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



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

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

//展开大小以处理glow
newSize.size.width + = 6.0;
newSize.size.height + = 6.0;
UIGraphicsBeginImageContext(newSize);
CGContextRef ctx = UIGraphicsGetCurrentContext();

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

//你可以重复这个过程来建立发光。
CGContextDrawImage(ctx,newSize,theImage);
CGContextSetAlpha(ctx,0.2);

CGContextEndTransparencyLayer(ctx);

//将原始图像绘制到上下文中,偏移为居中;
CGRect centerRect = inputImage.bounds;
centerRect.origin.x + = 3.0;
centerRect.origin.y + = 3.0;
CGContextDrawImage(ctx,centerRect,theImage);

result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

返回结果;然后在你的方法中缩放,你会做类似于:

  //假设UIImage * cachedImage = [self addGlowToImage:origImage];已被调用。 
//假设存在比例的ivars

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

[cachedImage drawInRect:newRect]; //图像将缩放以填充目标矩形。

一定要看看苹果文档。良好的起点是 Quartz 2D编程指南


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!!

解决方案

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.

这篇关于如何向UIImage或UIImageView或UIView添加外部光晕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆