如何在不使用UIImageView的情况下创建UIImage的反射?我已经看到了使用两个imageViews进行反射的苹果代码,但是我不想在我的应用程序中添加imageview,我只想要原始图像和反射图像的整个图像。有没有人知道如何做到这一点。没有UIImageView的UIImage反射
1
A
回答
1
这是来自的UIImage + FX.m,通过Nick Lockwood
UIImage * processedImage = //here your image
processedImage = [processedImage imageWithReflectionWithScale:0.15f
gap:10.0f
alpha:0.305f];
量表创建的反射的大小,间隙的图像和反射之间的距离,和α的反射的α
- (UIImage *)imageWithReflectionWithScale:(CGFloat)scale gap:(CGFloat)gap alpha:(CGFloat)alpha
{
//get reflected image
UIImage *reflection = [self reflectedImageWithScale:scale];
CGFloat reflectionOffset = reflection.size.height + gap;
//create drawing context
UIGraphicsBeginImageContextWithOptions(CGSizeMake(self.size.width, self.size.height + reflectionOffset * 2.0f), NO, 0.0f);
//draw reflection
[reflection drawAtPoint:CGPointMake(0.0f, reflectionOffset + self.size.height + gap) blendMode:kCGBlendModeNormal alpha:alpha];
//draw image
[self drawAtPoint:CGPointMake(0.0f, reflectionOffset)];
//capture resultant image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//return image
return image;
}
和
- (UIImage *)reflectedImageWithScale:(CGFloat)scale
{
//get reflection dimensions
CGFloat height = ceil(self.size.height * scale);
CGSize size = CGSizeMake(self.size.width, height);
CGRect bounds = CGRectMake(0.0f, 0.0f, size.width, size.height);
//create drawing context
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
//clip to gradient
CGContextClipToMask(context, bounds, [[self class] gradientMask]);
//draw reflected image
CGContextScaleCTM(context, 1.0f, -1.0f);
CGContextTranslateCTM(context, 0.0f, -self.size.height);
[self drawInRect:CGRectMake(0.0f, 0.0f, self.size.width, self.size.height)];
//capture resultant image
UIImage *reflection = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//return reflection image
return reflection;
}
gradientMask
+ (CGImageRef)gradientMask
{
static CGImageRef sharedMask = NULL;
if (sharedMask == NULL)
{
//create gradient mask
UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, 256), YES, 0.0);
CGContextRef gradientContext = UIGraphicsGetCurrentContext();
CGFloat colors[] = {0.0, 1.0, 1.0, 1.0};
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGGradientRef gradient = CGGradientCreateWithColorComponents(colorSpace, colors, NULL, 2);
CGPoint gradientStartPoint = CGPointMake(0, 0);
CGPoint gradientEndPoint = CGPointMake(0, 256);
CGContextDrawLinearGradient(gradientContext, gradient, gradientStartPoint,
gradientEndPoint, kCGGradientDrawsAfterEndLocation);
sharedMask = CGBitmapContextCreateImage(gradientContext);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
UIGraphicsEndImageContext();
}
return sharedMask;
}
它用反射返回图像。
0
您可以在图形上下文中渲染图像及其反射,然后从上下文中获取CGImage
,并从中再次获取UIImage
。 但问题是:为什么不使用两个视图?你为什么认为这是一个问题或限制?
1
我写了一个使用视图的CAReplicatorLayer的blog post awhile ago。它真的是设计用反射来处理视图的动态更新,但我认为它也适用于你想要做的事情。
相关问题
- 1. 具有底部反射的UIImage
- 2. UIImageview的反射图像
- 3. MVC4没有反射
- 4. UIImageView autoresizingmask,UIImage stretchableImageWithLeftCapWidth
- 5. UIImageView到UIImage?
- 6. UIImageView到UIImage
- 7. UIImageView和它的UIImage缩放比例没有额外的填充
- 8. 有UIImage填充UIImageView的整个框架
- 9. UIImageView内移动UIImage
- 10. 动画UIImage或UIImageView?
- 11. UIImage从UIImageView流血
- 12. 从UIImageView获取UIImage
- 13. 转换的UIImageView到的UIImage
- 14. 更新的UIImageView新的UIImage
- 15. 的UIImage为的UIImageView是零
- 16. UIImageView + AFNetworking获取UIImage的URL
- 17. UIImage和UIImageView的区别
- 18. 的UIImage在一个UIImageView
- 19. 粘UIButton到UIImageView的UIImage
- 20. UIImageView旋转我的UIImage
- 21. UIImageView中的UIImage大小
- 22. 为什么UIImageView自己的UIImage。它违反了MVC原则吗?
- 23. 在没有反射的java中xpath
- 24. 没有定义的反射和注释
- 25. 为UIImageView禁用大UIImage
- 26. 降低分辨率UIImage/UIImageView
- 27. 如何旋转UIImage或UIImageView?
- 28. 在UIImageView中安装UIImage
- 29. 使用UIImageView调整UIImage
- 30. 使用UIImageView时UIImage失真
什么是[[self class] gradientMask]?上面的代码会返回什么?反射图像或整体(原始+反射)图像? –
在答案上添加了gradientMask方法。 imageWithReflectionWithScale返回原始的+反射,reflectImageWithScale只返回反射。 – jcesarmobile
如何更改反射图像的高度?我想绘制原始图像作为视图的一半大小,另一半应该是下面的反射图像。即使我将阿尔法值设置为1反射图像,它也会在底部显示不透明的图像。 –