Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在Swift I'中裁剪图像后;我得到的结果倾斜90度-为什么?_Ios_Swift_Uiimage_Swift3 - Fatal编程技术网

Ios 在Swift I'中裁剪图像后;我得到的结果倾斜90度-为什么?

Ios 在Swift I'中裁剪图像后;我得到的结果倾斜90度-为什么?,ios,swift,uiimage,swift3,Ios,Swift,Uiimage,Swift3,我正在使用一个不错的github插件,用于Swift裁剪图像 我需要纵横比4:3,所以我这样称呼这个控制器: let shittyVC = ShittyImageCropVC(frame: (self.navigationController?.view.frame)!, image: image!, aspectWidth: 3, aspectHeight: 4) self.navigationController?.present(shittyVC, animated: true, comp

我正在使用一个不错的github插件,用于Swift裁剪图像

我需要纵横比4:3,所以我这样称呼这个控制器:

let shittyVC = ShittyImageCropVC(frame: (self.navigationController?.view.frame)!, image: image!, aspectWidth: 3, aspectHeight: 4)
self.navigationController?.present(shittyVC, animated: true, completion: nil)
现在,当我提供水平图像(宽于高)时,裁剪结果很好-我看到一张纵横比为4:3的照片作为输出

但当我提供垂直图像并尝试裁剪它时,我看到的是倾斜的输出。例如,当普通照片是这样的:

let shittyVC = ShittyImageCropVC(frame: (self.navigationController?.view.frame)!, image: image!, aspectWidth: 3, aspectHeight: 4)
self.navigationController?.present(shittyVC, animated: true, completion: nil)

垂直-和倾斜-一个看起来像这样:

let shittyVC = ShittyImageCropVC(frame: (self.navigationController?.view.frame)!, image: image!, aspectWidth: 3, aspectHeight: 4)
self.navigationController?.present(shittyVC, animated: true, completion: nil)

(很抱歉这里的分辨率太低)。为什么会移到一边

我怀疑问题可能出在“裁剪”按钮的逻辑中:

func tappedCrop() {
print("tapped crop")

var imgX: CGFloat = 0
if scrollView.contentOffset.x > 0 {
  imgX = scrollView.contentOffset.x / scrollView.zoomScale
}

let gapToTheHole = view.frame.height/2 - holeRect.height/2
var imgY: CGFloat = 0
if scrollView.contentOffset.y + gapToTheHole > 0 {
  imgY = (scrollView.contentOffset.y + gapToTheHole) / scrollView.zoomScale
}

let imgW = holeRect.width  / scrollView.zoomScale
let imgH = holeRect.height  / scrollView.zoomScale

print("IMG x: \(imgX) y: \(imgY) w: \(imgW) h: \(imgH)")

let cropRect = CGRect(x: imgX, y: imgY, width: imgW, height: imgH)
let imageRef = img.cgImage!.cropping(to: cropRect)
let croppedImage = UIImage(cgImage: imageRef!)

var path:String = NSTemporaryDirectory() + "tempFile.jpeg"


if let data = UIImageJPEGRepresentation(croppedImage, 0.95) { //0.4 - compression quality
    //print("low compression is here")
    try? data.write(to: URL(fileURLWithPath: path), options: [.atomic])

}

self.dismiss(animated: true, completion: nil)
}

ShittyImageCrop
将剪切的图像直接保存到相册中,我无法使用垂直图像复制您的问题

我看到您使用了
uiimagejpegresentation
UIImageWriteToSavedPhotosAlbum
中的
ShittyImageCrop
相比,似乎其他人在使用
uiimagejpegresentation
后也有图像旋转问题

仰望

编辑

尝试从中实现fixOrientation()

添加
fixOrientation()

然后在使用
uiimagejpegresentation
之前执行此操作:

func fixOrientation(img:UIImage) -> UIImage {
  if (img.imageOrientation == UIImageOrientation.Up) { 
    return img
  }

  UIGraphicsBeginImageContextWithOptions(img.size, false, img.scale)
  let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)
  img.drawInRect(rect)

  let normalizedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()
  return normalizedImage
}
if let data = UIImageJPEGRepresentation(fixOrientation(croppedImage), 0.95) {
  try? data.write(to: URL(fileURLWithPath: path), options: [.atomic])
}
编辑2

请将
img=image
替换为以下内容,编辑
ShittyImageCrop
init
方法:

if (image.imageOrientation != .up) {
  UIGraphicsBeginImageContextWithOptions(image.size, false, image.scale)
  var rect = CGRect.zero
  rect.size = image.size
  image.draw(in: rect)
  img = UIGraphicsGetImageFromCurrentImageContext()
  UIGraphicsEndImageContext()
} else {
  img = image
}

基本上你是说你正在使用你不理解的代码,当它不能像你期望的那样工作时,你也不理解。这对你正在使用的代码的作者来说不是个问题吗?@matt是的,没错:(问题是这个回购协议似乎被放弃了很长一段时间,我想在这里会是一个更好的地方来询问这个问题的根本原因……你知道代码中可能有什么错误吗?我试图自己修改它,但失败了,在这一刻,即使是最小的提示对我来说都是很好的。我不确定这是否适用于是代码,因为我没有使用滚动视图,但我相信当您使用
let cropedimage=
时,有一个方向选项。因此您可以单独尝试
let cropedimage:UIImage=UIImage(CGImage:imageRef,scale:imageRef.scale,orientation:imageRef.imageOrientation)
不确定是否有原始图像是您想要参考的方向,但是的,我可能会尝试类似的方法?谢谢您的快速回复!您完全正确,我的网站上的问题似乎是使用
UIImageJPEGRepresentation
。我在github上将我的照片上载到您的项目中,并它工作得很好!我之所以使用
uiimagejpegresentation
而不是您建议的
UIImageWriteToSavedPhotosAlbum
,是因为我需要获取图像的url路径以便进一步处理…在使用
UIImageWriteToSavedPhotosAlbum
时,有没有办法获取此url?@user3766930我已经更新了e答案,但实际上没有测试出来。我想你需要先旋转图像,然后再将其保存到文件中。你应该测试我上面链接的问题的一些答案。我注意到,在我使用iphone前摄像头拍摄的照片时,问题仍然存在,尽管我现在使用了你的功能来固定方向它仍然将图像保存到倾斜的照片中:(@user3766930很抱歉回复太晚了,但生活发生在我身上,所以我不得不推迟帮助你。我花了一些时间,终于找到了问题所在。我已经更新了我的答案,希望我也会更新ShittyImageCrop项目。谢谢你让我改进控制;)