iOS 图片左右反转 反向

http://blog.csdn.net/xyxjn/article/details/37902609

方法1

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

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


方法2

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  GYFlipLayer.h  
  3. //  imageFlipDemo  
  4. //  
  5. //  Created by sun on 14-7-17.  
  6. //  Copyright (c) 2014年 sun. All rights reserved.  
  7. //  
  8.   
  9. #import <QuartzCore/QuartzCore.h>  
  10.   
  11. @interface GYFlipLayer : CALayer  
  12.   
  13. - (id)initWithLayer:(CALayer *)layer;  
  14.   
  15. @end  

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. //  
  2. //  GYFlipLayer.m  
  3. //  imageFlipDemo  
  4. //  
  5. //  Created by sun on 14-7-17.  
  6. //  Copyright (c) 2014年 sun. All rights reserved.  
  7. //  
  8.   
  9. #import "GYFlipLayer.h"  
  10.   
  11. @interface GYFlipLayer()  
  12.   
  13. @property (strongnonatomicCALayer *reflectedLayer;  
  14.   
  15. @end  
  16.   
  17.   
  18. @implementation GYFlipLayer  
  19.   
  20. - (id)initWithLayer:(CALayer *)aLayer  
  21. {  
  22.     self = [super init];  
  23.       
  24.     if (self)  
  25.     {  
  26.         self.needsDisplayOnBoundsChange = YES;  
  27.         self.contentsScale = aLayer.contentsScale;  
  28.           
  29.         _reflectedLayer = aLayer;  
  30.         self.name = [NSString stringWithFormat:@"reflectionLayer%@", aLayer.name];  
  31.           
  32.         [self udpateFrame];  
  33.     }  
  34.       
  35.     return self;  
  36. }  
  37.   
  38. - (void)udpateFrame {  
  39.     CGRect frame = _reflectedLayer.bounds;  
  40.     self.frame = frame;  
  41. }  
  42.   
  43.   
  44. - (void)drawInContext:(CGContextRef)ctx  
  45. {  
  46.     CGContextSaveGState(ctx);  
  47.     CGContextSetInterpolationQuality(ctx, kCGInterpolationNone);  
  48.     CGContextTranslateCTM(ctx, self.reflectedLayer.frame.size.width0);  
  49.     CGContextScaleCTM(ctx, -1.f1.f);  
  50.       
  51.     [self.reflectedLayer renderInContext:ctx];  
  52.       
  53.     CGContextRestoreGState(ctx);  
  54. }  
  55.   
  56. @end  

调用

[objc]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. - (IBAction)flipImage:(id)sender {  
  2.     GYFlipLayer *rLayer = [[GYFlipLayer alloc] initWithLayer:_imageView.layer];  
  3.     [_imageView.layer addSublayer:rLayer];  
  4. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值