iOS 图片反转 反向

方法1

_imageView.transform = CGAffineTransformMakeScale(-1, 1);

弊端:和大小变化等动画不兼容


方法2

//
//  GYFlipLayer.h
//  imageFlipDemo
//
//  Created by sun on 14-7-17.
//  Copyright (c) 2014年 sun. All rights reserved.
//

#import 

@interface GYFlipLayer : CALayer

- (id)initWithLayer:(CALayer *)layer;

@end

//
//  GYFlipLayer.m
//  imageFlipDemo
//
//  Created by sun on 14-7-17.
//  Copyright (c) 2014年 sun. All rights reserved.
//

#import "GYFlipLayer.h"

@interface GYFlipLayer()

@property (strong, nonatomic) CALayer *reflectedLayer;

@end


@implementation GYFlipLayer

- (id)initWithLayer:(CALayer *)aLayer
{
	self = [super init];
    
	if (self)
	{
		self.needsDisplayOnBoundsChange = YES;
		self.contentsScale = aLayer.contentsScale;
        
		_reflectedLayer = aLayer;
		self.name = [NSString stringWithFormat:@"reflectionLayer%@", aLayer.name];
        
        [self udpateFrame];
	}
    
	return self;
}

- (void)udpateFrame {
    CGRect frame = _reflectedLayer.bounds;
    self.frame = frame;
}


- (void)drawInContext:(CGContextRef)ctx
{
	CGContextSaveGState(ctx);
    CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);
	CGContextTranslateCTM(ctx, self.reflectedLayer.frame.size.width, 0);
	CGContextScaleCTM(ctx, -1.f, 1.f);
    
	[self.reflectedLayer renderInContext:ctx];
    
	CGContextRestoreGState(ctx);
}

@end

调用

- (IBAction)flipImage:(id)sender {
    GYFlipLayer *rLayer = [[GYFlipLayer alloc] initWithLayer:_imageView.layer];
	[_imageView.layer addSublayer:rLayer];
}


你可能感兴趣的:(iOS技术)