Skip to content

Instantly share code, notes, and snippets.

@eddieespinal
Last active April 14, 2021 08:16

Revisions

  1. eddieespinal renamed this gist Mar 9, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. eddieespinal revised this gist Mar 9, 2016. 1 changed file with 1 addition and 4 deletions.
    5 changes: 1 addition & 4 deletions cropCameraImage
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    ``
    //This is the swift version of the following gist by @shexbeer https://gist.github.com/shexbeer/cb069d36ca8ec5edb515
    func cropCameraImage(original: UIImage, previewLayer: AVCaptureVideoPreviewLayer) -> UIImage? {

    @@ -30,6 +29,4 @@
    }

    return image
    }

    ``
    }
  3. eddieespinal revised this gist Mar 9, 2016. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion cropCameraImage
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    ``
    //This is the swift version of the following gist by @shexbeer https://gist.github.com/shexbeer/cb069d36ca8ec5edb515
    func cropCameraImage(original: UIImage, previewLayer: AVCaptureVideoPreviewLayer) -> UIImage? {

    @@ -29,4 +30,6 @@
    }

    return image
    }
    }

    ``
  4. eddieespinal revised this gist Mar 9, 2016. 1 changed file with 1 addition and 5 deletions.
    6 changes: 1 addition & 5 deletions cropCameraImage
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,3 @@
    ```swift

    //This is the swift version of the following gist by @shexbeer https://gist.github.com/shexbeer/cb069d36ca8ec5edb515
    func cropCameraImage(original: UIImage, previewLayer: AVCaptureVideoPreviewLayer) -> UIImage? {

    @@ -31,6 +29,4 @@
    }

    return image
    }

    ```
    }
  5. eddieespinal revised this gist Mar 9, 2016. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion cropCameraImage
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ```javascript
    ```swift

    //This is the swift version of the following gist by @shexbeer https://gist.github.com/shexbeer/cb069d36ca8ec5edb515
    func cropCameraImage(original: UIImage, previewLayer: AVCaptureVideoPreviewLayer) -> UIImage? {
    @@ -32,4 +32,5 @@

    return image
    }

    ```
  6. eddieespinal revised this gist Mar 9, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions cropCameraImage
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    ```javascript

    //This is the swift version of the following gist by @shexbeer https://gist.github.com/shexbeer/cb069d36ca8ec5edb515
    func cropCameraImage(original: UIImage, previewLayer: AVCaptureVideoPreviewLayer) -> UIImage? {

  7. eddieespinal revised this gist Mar 9, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion cropCameraImage
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    ```swift
    ```javascript
    //This is the swift version of the following gist by @shexbeer https://gist.github.com/shexbeer/cb069d36ca8ec5edb515
    func cropCameraImage(original: UIImage, previewLayer: AVCaptureVideoPreviewLayer) -> UIImage? {

  8. eddieespinal revised this gist Mar 9, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion cropCameraImage
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    ```swift
    //This is the swift version of the following gist by @shexbeer https://gist.github.com/shexbeer/cb069d36ca8ec5edb515
    func cropCameraImage(original: UIImage, previewLayer: AVCaptureVideoPreviewLayer) -> UIImage? {

    @@ -29,4 +30,5 @@
    }

    return image
    }
    }
    ```
  9. eddieespinal created this gist Mar 9, 2016.
    32 changes: 32 additions & 0 deletions cropCameraImage
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    //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
    }