How do I add an outer glow to a UIImage or UIImageView or UIView(如何向 UIImage 或 UIImageView 或 UIView 添加外发光)
问题描述
我想为 UIImage
/UIImageView
/UIView
添加 FADED 阴影/外发光,但我不知道 Core Graphics
完全没有.
I want to add a FADED shadow/outer glow to a UIImage
/UIImageView
/UIView
but I know no Core Graphics
at all.
请帮忙!!
推荐答案
采用 Cirrostratus 概述的方法,保留它的缓存副本,然后在拖动时应用变换来更改图像的大小和/或位置.
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.
一定要看看苹果文档.Quartz 2D Programming Guide.
Definitely take a look at the apple docs. A good starting place is the Quartz 2D Programming Guide.
这篇关于如何向 UIImage 或 UIImageView 或 UIView 添加外发光的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何向 UIImage 或 UIImageView 或 UIView 添加外发光
基础教程推荐
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01