Brejuro
Brejuro

Reputation: 3541

UIImage getting rotated 90 degrees after cropping

I have the following function to crop a portion of a UIImage and return the cropped UIImage. However, when using this cropped UIImage, it has been rotated 90 degrees. My image is usually in "portrait mode" I guess you can say, and I'm trying to keep it like that after cropping.

I'm aware there are other posts with this problem but I have tried to implement solutions and none have worked for me.

  private func cropImage(image: UIImage, cropRect: CGRect) -> UIImage {
        UIGraphicsBeginImageContextWithOptions(cropRect.size, false, 0);
        let context = UIGraphicsGetCurrentContext();

        context?.translateBy(x: 0.0, y: image.size.height);
        context?.scaleBy(x: 1.0, y: -1.0);
        context?.draw(image.cgImage!, in: CGRect(x:0, y:0, width:image.size.width, height:image.size.height), byTiling: false);
        context?.clip(to: [cropRect]);

        let croppedImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        return croppedImage!;
    }

Original UIImage created by the following:

let imageData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(sampleBuffer!)
let dataProvider = CGDataProvider(data: imageData as CFData)
let cgImageRef = CGImage(jpegDataProviderSource: dataProvider!, decode: nil, shouldInterpolate: true, intent: CGColorRenderingIntent.defaultIntent)

let image = UIImage(cgImage: cgImageRef!, scale: 1.0, orientation: self.getImageOrientation(forCamera: self.currentCamera))

Upvotes: 0

Views: 942

Answers (1)

matt
matt

Reputation: 535229

What you're doing is a very odd way to crop. Passing out of the UIImage world into the CGImage world lets you in for all sorts of difficulties (as you've discovered); you lose scaling and orientation information, and the image can end up flipped vertically. The normal way is more like this:

UIGraphicsBeginImageContextWithOptions(cropRect.size, false, 0)
image.draw(at:CGPoint(x:-cropRect.origin.x, y:-cropRect.origin.y))
let croppedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

Also note that starting in iOS 10 the entire UIGraphicsBeginImageContextWithOptions dance is outmoded, as you can use a UIGraphicsImageRenderer instead.

Upvotes: 1

Related Questions