我想知道有没有什么方法可以镜像一个镜像。例如,给某人的脸拍一张照片,然后把它切成两半,然后显示出他们的脸在两侧镜像后的样子。在CGAffineTransform函数中似乎没有这样的技巧。图形专家请帮帮忙!
发布于 2010-06-28 13:21:35
这里的基本“技巧”是使用因子为-1的关于X或Y轴的缩放变换。例如,您可以使用此命令创建“围绕水平轴翻转”变换:
CGAffineTransform transform = CGAffineTransformScale(transform, -1, 1);
然后,您可以在UIImageView
上设置transform
属性以翻转指定的图像,或将其与另一个变换连接以实现更复杂的效果。要获得您所描述的确切效果,您可能需要编写一些自定义绘制代码来将原始图像绘制到上下文中,然后将翻转的一半覆盖在它的顶部。这在Core Graphics中相对简单。
发布于 2010-06-29 00:23:41
如果您只计划支持4.0+
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];
发布于 2013-02-26 02:38:21
您可能会想,为什么要为超长的switch语句而烦恼呢?
? UIImage *flip = [UIImage imageWithCGImage:image.CGImage
? scale:image.scale
? orientation:(image.imageOrientation + 4) % 8];
如果你看一下枚举,你会发现模运算可以做到:
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语句编写函数。例如。
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;
}
}
并像这样使用函数:
UIImage *flip = [UIImage imageWithCGImage:image.CGImage
scale:image.scale
orientation:mirroredImageOrientation(image.imageOrientation)];
我添加了问号来表示有问题的、难闻的代码。类似于The Practice of Programming
https://stackoverflow.com/questions/3129282
复制相似问题