首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何从UIImagePickerController镜像UIImage图片

如何从UIImagePickerController镜像UIImage图片
EN

Stack Overflow用户
提问于 2010-06-28 07:24:55
回答 6查看 24.8K关注 0票数 18

我想知道有没有什么方法可以镜像一个镜像。例如,给某人的脸拍一张照片,然后把它切成两半,然后显示出他们的脸在两侧镜像后的样子。在CGAffineTransform函数中似乎没有这样的技巧。图形专家请帮帮忙!

EN

回答 6

Stack Overflow用户

发布于 2010-06-28 13:21:35

这里的基本“技巧”是使用因子为-1的关于X或Y轴的缩放变换。例如,您可以使用此命令创建“围绕水平轴翻转”变换:

代码语言:javascript
复制
CGAffineTransform transform = CGAffineTransformScale(transform, -1, 1);

然后,您可以在UIImageView上设置transform属性以翻转指定的图像,或将其与另一个变换连接以实现更复杂的效果。要获得您所描述的确切效果,您可能需要编写一些自定义绘制代码来将原始图像绘制到上下文中,然后将翻转的一半覆盖在它的顶部。这在Core Graphics中相对简单。

票数 40
EN

Stack Overflow用户

发布于 2010-06-29 00:23:41

如果您只计划支持4.0+

代码语言:javascript
复制
UIImageOrientation flippedOrientation = UIImageOrientationUpMirrored;
switch (image.imageOrientation) {
  case UIImageOrientationUp: break;
  case UIImageOrientationDown: flippedOrientation = UIImageOrientationDownMirrored; break;
  // ...
}
UIImage * flippedImage = [UIImage imageWithCGImage:image.CGImage scale:image.scale orientation:flippedOrientation];
票数 19
EN

Stack Overflow用户

发布于 2013-02-26 02:38:21

您可能会想,为什么要为超长的switch语句而烦恼呢?

代码语言:javascript
复制
? UIImage *flip = [UIImage imageWithCGImage:image.CGImage   
?                                     scale:image.scale
?                               orientation:(image.imageOrientation + 4) % 8];

如果你看一下枚举,你会发现模运算可以做到:

代码语言:javascript
复制
typedef NS_ENUM(NSInteger, UIImageOrientation) {
    UIImageOrientationUp,            // default orientation
    UIImageOrientationDown,          // 180 deg rotation
    UIImageOrientationLeft,          // 90 deg CCW
    UIImageOrientationRight,         // 90 deg CW
    UIImageOrientationUpMirrored,    // as above but image mirrored along other axis. horizontal flip
    UIImageOrientationDownMirrored,  // horizontal flip
    UIImageOrientationLeftMirrored,  // vertical flip
    UIImageOrientationRightMirrored, // vertical flip
};

但是这段代码太聪明了。您应该改为使用显式switch语句编写函数。例如。

代码语言:javascript
复制
UIImageOrientation mirroredImageOrientation(UIImageOrientation orientation) {
    switch(orientation) {
        case UIImageOrientationUp: return UIImageOrientationUpMirrored;
        case UIImageOrientationDown: return UIImageOrientationDownMirrored;
        case UIImageOrientationLeft: return UIImageOrientationLeftMirrored;
        case UIImageOrientationRight: return UIImageOrientationRightMirrored;
        case UIImageOrientationUpMirrored: return UIImageOrientationUp;
        case UIImageOrientationDownMirrored: return UIImageOrientationDown;
        case UIImageOrientationLeftMirrored: return UIImageOrientationLeft;
        case UIImageOrientationRightMirrored: return UIImageOrientationRight;
        default: return orientation;
    }
}

并像这样使用函数:

代码语言:javascript
复制
UIImage *flip = [UIImage imageWithCGImage:image.CGImage   
                                    scale:image.scale
                              orientation:mirroredImageOrientation(image.imageOrientation)];

我添加了问号来表示有问题的、难闻的代码。类似于The Practice of Programming

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3129282

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档