我无法将图像快速旋转90度.我写了下面的代码,但有一个错误,不编译
func imageRotatedByDegrees(oldImage: UIImage, deg degrees: CGFloat) -> UIImage {
//Calculate the size of the rotated view's containing box for our drawing space
let rotatedViewBox: UIView = UIView(frame: CGRect(x: 0, y: 0, width: oldImage.size.width, height: oldImage.size.height))
let t: CGAffineTransform = CGAffineTransform(rotationAngle: degrees * CGFloat(M_PI / 180))
rotatedViewBox.transform = t
let rotatedSize: CGSize = rotatedViewBox.frame.size
//Create the bitmap context
UIGraphicsBeginImageContext(rotatedSize)
let bitmap: CGContext = UIGraphicsGetCurrentContext()!
//Move the origin to the middle of the image so we will rotate and scale around …Run Code Online (Sandbox Code Playgroud) 进行UIImage翻转的解决方案是使用Objective-C代码:
[UIImage imageWithCGImage:img.CGImage scale:1.0 orientation: UIImageOrientationDownMirrored]
Run Code Online (Sandbox Code Playgroud)
但是,在Swift中没有imageWithCGImage!是否有使用Swift水平翻转图像的解决方案?谢谢!
StackOverflow上有几个问题涉及图像翻转,例如这里的图像翻转.默认情况下,iOS会在拍摄照片时反转前置摄像头水平图像.我试图防止前置摄像头图像被翻转或将其翻转回正确的方向.我正在与...互动WKWebview.
问题是我不知道调用或放入我的ViewController相机的方法,然后将其设置为正确的方向,或正确的设置以防止此行为.我也不知道如何获取拍摄图像的相机信息.
这是我尝试的一种解决方案,它基于翻译一些Objective-C代码,在相机完成照片后更改图像.但是图片变量是常量,不能更改:
func didTakePicture(_ picture: UIImage) {
var flippedImage = UIImage(cgImage: picture.cgImage!, scale: picture.scale, orientation: .leftMirrored)
picture = flippedImage
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.谢谢!
我想以顺时针方向旋转UIImage.但是我的当前代码在某些时候旋转并且有时将其跳过旋转时执行功能不准确.我希望我的UIImage将在我的动作按钮上顺时针旋转.这是我目前的代码:
imageView.image = imageView.image!.imageRotatedByDegrees(angle, flip: false)
angle = angle + 90
if angle > 360{
angle = 0
}
Run Code Online (Sandbox Code Playgroud) 我正在使用 UIImagePickerController 从相机捕获照片或从照片库中选择,然后显示图像。这适用于横向/水平方向的照片,但纵向/垂直方向的照片显示为水平拉伸。
我试过 .scaledToFit() 和 .aspectRatio(contentMode: .fit),但它仍然显示拉伸。很感谢任何形式的帮助。
显示图片的代码:
struct AddNewItem: View {
@State private var showImagePicker: Bool = true
@State private var image: UIImage = nil
@State var showCamera: Bool = false
@State private var showImageEditor: Bool = false
var body: some View {
VStack {
Image(uiImage: image ?? UIImage(named: "placeholder")!)
.resizable()
.scaledToFit()
.aspectRatio(contentMode: .fit)
}
//Show the photo capture view
.sheet(isPresented: self.$showImagePicker) {
PhotoCaptureView(showImagePicker: self.$showImagePicker, image: self.$image, showImageEditor: self.$showImageEditor, showCamera: self.$showCamera)
}
}
Run Code Online (Sandbox Code Playgroud)
}
图像选择器协调员:
class ImagePickerCoordinator: …Run Code Online (Sandbox Code Playgroud)