2012-09-20 50 views
1

如何在不使用UIImageView的情况下创建UIImage的反射?我已经看到了使用两个imageViews进行反射的苹果代码,但是我不想在我的应用程序中添加imageview,我只想要原始图像和反射图像的整个图像。有没有人知道如何做到这一点。没有UIImageView的UIImage反射

回答

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

什么是[[self class] gradientMask]?上面的代码会返回什么?反射图像或整体(原始+反射)图像? –

+0

在答案上添加了gradientMask方法。 imageWithReflectionWithScale返回原始的+反射,reflectImageWithScale只返回反射。 – jcesarmobile

+0

如何更改反射图像的高度?我想绘制原始图像作为视图的一半大小,另一半应该是下面的反射图像。即使我将阿尔法值设置为1反射图像,它也会在底部显示不透明的图像。 –

0

您可以在图形上下文中渲染图像及其反射,然后从上下文中获取CGImage,并从中再次获取UIImage。 但问题是:为什么不使用两个视图?你为什么认为这是一个问题或限制?

1

我写了一个使用视图的CAReplicatorLayer的blog post awhile ago。它真的是设计用反射来处理视图的动态更新,但我认为它也适用于你想要做的事情。