Skip to content
This repository was archived by the owner on Jul 1, 2022. It is now read-only.

Commit 50960d1

Browse files
Crop correctly in portrait orientation with library access off.
1 parent 59b8363 commit 50960d1

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

ALCameraViewController/ViewController/ConfirmViewController.swift

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,33 @@ public class ConfirmViewController: UIViewController, UIScrollViewDelegate {
244244
}
245245
.setAsset(asset)
246246
} else {
247-
self.onComplete?(image, nil)
247+
var newImage = image
248+
249+
if allowsCropping {
250+
var cropRect = cropOverlay.frame
251+
cropRect.origin.x += scrollView.contentOffset.x
252+
cropRect.origin.y += scrollView.contentOffset.y
253+
254+
let normalizedX = cropRect.origin.x / imageView.frame.width
255+
let normalizedY = cropRect.origin.y / imageView.frame.height
256+
257+
let normalizedWidth = cropRect.width / imageView.frame.width
258+
let normalizedHeight = cropRect.height / imageView.frame.height
259+
260+
261+
let normRect = normalizedRect(CGRect(x: normalizedX, y: normalizedY, width: normalizedWidth, height: normalizedHeight), orientation: imageView.image!.imageOrientation)
262+
print("NormRect: \(normRect)")
263+
264+
let newRect = CGRect(x: (imageView.image!.size.width) * (1 - (normRect.origin.y + normRect.height)),
265+
y: (imageView.image!.size.height) * normRect.origin.x,
266+
width: (imageView.image!.size.width * normRect.height),
267+
height: (imageView.image!.size.height * normRect.width))
268+
// Note: Mixing x with height and y with width and flipping y because our image orientation is off by 90 degrees because the iPad's in portrait. In landscape, this fails miserably.
269+
print("NewRect: \(newRect)")
270+
newImage = imageView.image?.crop(rect: newRect)
271+
}
272+
273+
self.onComplete?(newImage, nil)
248274
self.hideSpinner(spinner)
249275
self.enable()
250276
}
@@ -311,3 +337,30 @@ public class ConfirmViewController: UIViewController, UIScrollViewDelegate {
311337
}
312338

313339
}
340+
341+
extension UIImage {
342+
func crop(rect: CGRect) -> UIImage {
343+
let rad = {(_ deg: CGFloat) -> CGFloat in
344+
return deg / 180.0 * .pi
345+
}
346+
var rectTransform: CGAffineTransform
347+
switch imageOrientation {
348+
case .left:
349+
rectTransform = CGAffineTransform(rotationAngle: rad(90)).translatedBy(x: 0, y: -size.height)
350+
case .right:
351+
rectTransform = CGAffineTransform(rotationAngle: rad(-90)).translatedBy(x: -size.width, y: 0)
352+
case .down:
353+
rectTransform = CGAffineTransform(rotationAngle: rad(-180)).translatedBy(x: -size.width, y: -size.height)
354+
default:
355+
rectTransform = CGAffineTransform.identity
356+
}
357+
358+
rectTransform = rectTransform.scaledBy(x: scale, y: scale)
359+
360+
if let cropped = cgImage?.cropping(to: rect.applying(rectTransform)) {
361+
return UIImage(cgImage: cropped, scale: scale, orientation: imageOrientation)
362+
}
363+
364+
return self
365+
}
366+
}

0 commit comments

Comments
 (0)