【发布时间】:2010-02-17 05:02:31
【问题描述】:
我想为UIImage/UIImageView/UIView 添加 FADED 阴影/外发光,但我根本不知道 Core Graphics。
编辑: 请帮忙!!
【问题讨论】:
标签: iphone cocoa-touch uiimageview uiimage core-graphics
我想为UIImage/UIImageView/UIView 添加 FADED 阴影/外发光,但我根本不知道 Core Graphics。
编辑: 请帮忙!!
【问题讨论】:
标签: iphone cocoa-touch uiimageview uiimage 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。
【讨论】:
你可以使用这个,简单快速,使用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;
【讨论】:
出于性能考虑,iPhone OS 不支持通常可以在 Cocoa 中使用的 shadowColor、shadowOffset、shadowOpacity 和 shadowRadius 属性。大多数人多次复制他们想要发光的形状,每次降低不透明度并一次偏移一个像素以模拟发光的外观。如果您的发光不需要很大,您就无法区分。
【讨论】:
touchesMoved 事件更改大小。