views:

630

answers:

3

I just want the easiest way to make a reflection under a UIImageVies that is easily managable.

A: 

The easiest way is to do it by hand in Photoshop! (seriously - if that's practical just do that).

Otherwise you'll need to make an inverted copy of the image (or at least the bottom half) to place below the real one, overlaid with a gradient from black (assuming your background is black) with alpha=1 to alpha=0.

If you need to place it over arbitrary backgrounds it's a little more complex as you'll have to apply the gradient from alpha = 0 to alpha = 1 to your inverted image. I knocked up some code once to do it - will try and dig it out later when I get to my Mac, if nobody else has come up with anything sooner.

Phil Nash
+2  A: 

Just use the sample code in the the iPhone SDK library

JBRWilkinson
Cool, that wasn't there last time I had to do this :-)
Phil Nash
+1  A: 

As Phil says, you can have a "reflected" UIImageView instance:

@interface ReflectedImageView : UIView 
{
@private
    UIImageView *_imageView;
    UIImageView *_imageReflectionView;
}

@property (nonatomic, retain) UIImage *image;

@end

And then, in your implementation, something like this

@implementation ReflectedImageView

@dynamic image;

- (id)initWithFrame:(CGRect)frame 
{
    if (self = [super initWithFrame:frame]) 
    {
        self.backgroundColor = [UIColor clearColor];

        // This should be the size of your image:
        CGRect rect = CGRectMake(0.0, 0.0, 320.0, 290.0);

        _imageReflectionView = [[UIImageView alloc] initWithFrame:rect];
        _imageReflectionView.contentMode = UIViewContentModeScaleAspectFit;
        _imageReflectionView.alpha = 0.4;
        _imageReflectionView.transform = CGAffineTransformMake(1, 0, 0, -1, 0, 290.0);
        [self addSubview:_imageReflectionView];

        _imageView = [[UIImageView alloc] initWithFrame:rect];
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:_imageView];
    }
    return self;
}

- (void)setImage:(UIImage *)newImage
{
    _imageView.image = newImage;
    _imageReflectionView.image = newImage;
}

- (UIImage *)image
{
    return _imageView.image;
}

- (void)dealloc 
{
    [_imageView release];
    [_imageReflectionView release];
    [super dealloc];
}

@end
Adrian Kosmaczewski
Why are you changing the alpha of the reflected image, Then returning only one image?
Jaba
I change the alpha to create a kind of "metallic" reflection effect :) As for the property getter, given that both image views receive the same image, returning either one of them is the same, and I chose to return the image from _imageView, but it's an arbitrary choice. Thanks for selecting my answer!
Adrian Kosmaczewski
That's most of the way there. It doesn't include the "fade" effect, however.
Phil Nash
Yeah, that's what I ment
Jaba