UIImage水平翻转,垂直翻转

1,实现原理
UIImage 有个属性叫 imageOrientation,它是一个枚举变量。主要作用是控制image的绘制方向,共有以下8种方向

typedef NS_ENUM(NSInteger, UIImageOrientation) {
    UIImageOrientationUp,            // 默认方向
    UIImageOrientationDown,          // 180°旋转
    UIImageOrientationLeft,          // 逆时针旋转90°
    UIImageOrientationRight,         // 顺时针旋转90°
    UIImageOrientationUpMirrored,    // 垂直翻转(向上)
    UIImageOrientationDownMirrored,  // 垂直翻转(向下)
    UIImageOrientationLeftMirrored,  // 水平翻转(向左)
    UIImageOrientationRightMirrored, // 水平翻转 (向右)
};

那么我们只需要通过改变 UIImage 的 orientation,这样图片显示出来的时候,图片容器会根据新的这个 orientation 属性进行显示。从而实现水平翻转或者垂直翻转。

2,垂直翻转(上下翻转)

     UIImageView *backImage = [[UIImageView alloc]init];
    backImage.frame = CGRectMake(0, -300, KSCREEN_WIDTH, 300);
    backImage.image = [UIImage imageNamed:@"你图片的名称"];
    
    UIImage *flipImage = [UIImage imageWithCGImage:backImage.image.CGImage scale:backImage.image.scale orientation:UIImageOrientationDownMirrored];
    backImage.image = flipImage;

3,水平翻转(即左右镜像)

    UIImageView *backImage = [[UIImageView alloc]init];
    backImage.frame = CGRectMake(0, -300, KSCREEN_WIDTH, 300);
    backImage.image = [UIImage imageNamed:@"你图片的名称"];
    
    UIImage *flipImage = [UIImage imageWithCGImage:backImage.image.CGImage scale:backImage.image.scale orientation:UIImageOrientationLeftMirrored];
    backImage.image = flipImage;


参考链接:https://www.jianshu.com/p/e21a0ed779c1

posted on 2015-11-07 23:25  🌞Bob  阅读(345)  评论(0编辑  收藏  举报

导航