Skip to content

Instantly share code, notes, and snippets.

@eddieespinal
Last active April 14, 2021 08:16
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddieespinal/a7bb8a97ac3e935af1fb to your computer and use it in GitHub Desktop.
Save eddieespinal/a7bb8a97ac3e935af1fb to your computer and use it in GitHub Desktop.
Crops a Picture from AVCaptureSession to the bounds of the AVCaptureVideoPreviewLayer (so Preview = CameraImage) - Swift Version
//This is the swift version of the following gist by @shexbeer https://gist.github.com/shexbeer/cb069d36ca8ec5edb515
func cropCameraImage(original: UIImage, previewLayer: AVCaptureVideoPreviewLayer) -> UIImage? {
var image = UIImage()
let previewImageLayerBounds = previewLayer.bounds
let originalWidth = original.size.width
let originalHeight = original.size.height
let A = previewImageLayerBounds.origin
let B = CGPointMake(previewImageLayerBounds.size.width, previewImageLayerBounds.origin.y)
let D = CGPointMake(previewImageLayerBounds.size.width, previewImageLayerBounds.size.height)
let a = previewLayer.captureDevicePointOfInterestForPoint(A)
let b = previewLayer.captureDevicePointOfInterestForPoint(B)
let d = previewLayer.captureDevicePointOfInterestForPoint(D)
let posX = floor(b.x * originalHeight)
let posY = floor(b.y * originalWidth)
let width: CGFloat = d.x * originalHeight - b.x * originalHeight
let height: CGFloat = a.y * originalWidth - b.y * originalWidth
let cropRect = CGRectMake(posX, posY, width, height)
if let imageRef = CGImageCreateWithImageInRect(original.CGImage, cropRect) {
image = UIImage(CGImage: imageRef, scale: 2.5, orientation: .LeftMirrored)
}
return image
}
@lauthu
Copy link

lauthu commented Oct 26, 2016

mysterious .LeftMirrored. Thank you.

@pelluch
Copy link

pelluch commented Nov 24, 2017

Thank you.
Here's the Swift 3/4 version in case anyone needs it:

    func cropCameraImage(original: UIImage, previewLayer: AVCaptureVideoPreviewLayer) -> UIImage? {
        
        var image = UIImage()
        
        let previewImageLayerBounds = previewLayer.bounds
        
        let originalWidth = original.size.width
        let originalHeight = original.size.height
        
        let A = previewImageLayerBounds.origin
        let B = CGPoint(x: previewImageLayerBounds.size.width, y: previewImageLayerBounds.origin.y)
        let D = CGPoint(x: previewImageLayerBounds.size.width, y: previewImageLayerBounds.size.height)
        
        let a = previewLayer.captureDevicePointConverted(fromLayerPoint: A)
        let b = previewLayer.captureDevicePointConverted(fromLayerPoint: B)
        let d = previewLayer.captureDevicePointConverted(fromLayerPoint: D)
        
        let posX = floor(b.x * originalHeight)
        let posY = floor(b.y * originalWidth)
        
        let width: CGFloat = d.x * originalHeight - b.x * originalHeight
        let height: CGFloat = a.y * originalWidth - b.y * originalWidth
        
        let cropRect = CGRect(x: posX, y: posY, width: width, height: height)
        
        if let imageRef = original.cgImage?.cropping(to: cropRect) {
            image = UIImage(cgImage: imageRef, scale: 2.5, orientation: .right)
        }
        
        return image
    }

@cristinaITdeveloper
Copy link

cristinaITdeveloper commented Jan 5, 2018

I used this code in my app, but the width and height are 0 (iPhone X with full screen landscape camera)

schermata 2018-01-05 alle 16 47 36

How I can fix?

@theLeroy
Copy link

theLeroy commented Jul 4, 2019

have you solved this problem? @cristinaITdeveloper if so. how?

@hck916
Copy link

hck916 commented Apr 14, 2021

tank you ``

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment