如何从 UIImagePickerController 镜像 UIImage 图片
我想弄清楚是否有任何方法可以镜像图像。例如,拍摄某人脸部的照片,然后将其切成两半,并显示他们的脸部在每一面都镜像后的样子。 CGAffineTransform 函数中似乎没有类似的技巧。请各位图形专家帮忙!!!
I'm trying to figure out if there is any way to mirror an image. For example, take a picture of someone's face and then cut it in half and show what their face looks like with each side mirrored. There doesn't seem to be any tricks like this in CGAffineTransform functions. Graphics experts please help!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这里的基本“技巧”是使用关于 X 或 Y 轴的缩放变换,系数为 -1。例如,您可以使用它来创建“绕水平轴翻转”变换:
然后您可以在
UIImageView
上设置transform
属性来翻转指定的图像,或者将其与另一个变换连接起来以实现更复杂的效果。为了获得您所描述的确切效果,您可能需要编写一些自定义绘图代码以将原始图像绘制到上下文中,然后将翻转的一半覆盖在其顶部。这在 Core Graphics 中相对简单。The basic "trick" here is to use a scaling transform about the X or Y axis with a factor of -1. For example, you could use this to create a "flip about the horizontal axis" transform:
Then you can set the
transform
property on aUIImageView
to flip the assigned image, or concatenate it with another transform to do more sophisticated effects. To get the exact effect you described, you may need to write some custom drawing code to draw your original image into a context, then overlay the flipped half on top of it. This is relatively straightforward in Core Graphics.如果您只打算支持 4.0+
If you only plan on supporting 4.0+
您可能会想,为什么要费心冗长的 switch 语句呢?
如果你看一下枚举,你会发现模运算就可以了:
但是这段代码太聪明了。您应该编写一个带有显式 switch 语句的函数。例如
,使用这样的函数:
我添加了问号来指示有问题的、有臭味的代码。类似于 编程实践
You may think, why bother with the outrageously long switch statement?
And if you take a look at the enum you can see that modular arithmetic would do:
But this code is too clever. You should write a function with an explicit switch statement instead. E.g.
And use the function like this:
I've added question marks to indicate questionable, smelly code. Similar to The Practice of Programming
上面的答案都没有回答问题中镜像一半图像而不是翻转整个图像的部分。混合解决方案会产生以下示例函数,您可以将其用作类别,例如 UIImage+Mirroring :
None of the answers above, respond to the part of question that is mirroring half of the image not flipping the whole image. Mixing the solutions leads to the following sample function you may use as a category such as UIImage+Mirroring :
使用更简单:
It's easier to just use:
您可以在 Swift 4.0 中使用以下代码
You can use below code for Swift 4.0