user3766930
user3766930

Reputation: 5829

After cropping images in Swift I'm getting results tilted with 90 degrees - why?

I'm using a nice github plugin for Swift https://github.com/budidino/ShittyImageCrop responsible for cropping the image.

I need aspect ratio 4:3, so I call this controller like this:

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

Now, when I provide horizontal image (wider than taller) - cropped result is fine - I see a photo with aspect ratio 4:3 as an output.

But when I provide vertical image and try to cropp it - I'm seeing tilted output. So for example, when normal photo is like this:

enter image description here

vertical - and tilted - one looks like this:

enter image description here

(sorry for low res here). Why does it get shifted to one side?

I suspect the problem might be somewhere in the logic of the crop-button:

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)
}

Upvotes: 2

Views: 1110

Answers (1)

budiDino
budiDino

Reputation: 13527

ShittyImageCrop saves cropped images directly to your album and I couldn't replicate your issue using vertical images.

I see you used UIImageJPEGRepresentation compared to UIImageWriteToSavedPhotosAlbum from ShittyImageCrop and it seems other people also have problems with image rotation after using UIImageJPEGRepresentation.

Look up iOS UIImagePickerController result image orientation after upload and iOS JPEG images rotated 90 degrees

EDIT

try implementing fixOrientation() from https://stackoverflow.com/a/27775741/611879

add fixOrientation():

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
}

and then do it before using UIImageJPEGRepresentation:

if let data = UIImageJPEGRepresentation(fixOrientation(croppedImage), 0.95) {
  try? data.write(to: URL(fileURLWithPath: path), options: [.atomic])
}

EDIT 2

please edit the init method of ShittyImageCrop by replacing img = image with:

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
}

Upvotes: 1

Related Questions