0

I have UIImageView placed in UIScrollView. This UIScrollView can scroll in vertical and horizontal directions, can zoom and can rotate. UIScrollView is pinned in Storyboard top/left/right/bottom to superview and UIImageView is pinned top/left/rigth/bottom to UIScrollView.

In a code I set:

override func viewDidLoad() {
    super.viewDidLoad()

    let rotationGesture = UIRotationGestureRecognizer(target: self, action: #selector(didRotateSelectedPhoto(_:)))
    rotationGesture.delegate = self
    selectedPhotoScrollView.addGestureRecognizer(rotationGesture)
}

func didRotateSelectedPhoto(_ sender: UIRotationGestureRecognizer) {
    if sender.state == .began {
        lastRotation = 0.0
    }
    else {
        let rotation = 0.0 - (lastRotation - sender.rotation)
        selectedPhotoScrollView.transform = selectedPhotoScrollView.transform.rotated(by: rotation)
        lastRotation = sender.rotation
    }
}

fileprivate func updateSelectedPhotoView(withPhoto photo: UIImage) {
    selectedPhotoImageView.image = photo

    let minZoomScale: CGFloat
    if cropPortraitContainerView.isHidden {
        minZoomScale = photosContainerView.frame.width / photo.size.width
        selectedPhotoScrollView.contentInset = UIEdgeInsetsMake(landscapeCropHeightCostraint.constant, 0.0, landscapeCropHeightCostraint.constant, 0.0)
    }
    else {
        minZoomScale = photosContainerView.frame.height / photo.size.height
        selectedPhotoScrollView.contentInset = UIEdgeInsetsMake(0.0, portraitCropWidthConstraint.constant, 0.0, portraitCropWidthConstraint.constant)
    }

    selectedPhotoScrollView.minimumZoomScale = minZoomScale
    selectedPhotoScrollView.maximumZoomScale = 1.2
    selectedPhotoScrollView.setZoomScale(minZoomScale, animated: false)
}

extension PhotoPickerViewController: UIScrollViewDelegate {

    func viewForZooming(in scrollView: UIScrollView) -> UIView? {
        return selectedPhotoImageView
    }
}

extension PhotoPickerViewController: UIGestureRecognizerDelegate {

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true
    }
}

Panning, zooming rotating works perfectly. Bu my goal is to crop this image.

Here is my view:

enter image description here

I can very easy crop panned and zoomed image:

let originX: CGFloat
let originY: CGFloat
let width: CGFloat
let height: CGFloat
if cropPortraitContainerView.isHidden {
    originX = selectedPhotoScrollView.contentOffset.x / selectedPhotoScrollView.zoomScale
    originY = (selectedPhotoScrollView.contentOffset.y + landscapeCropHeightCostraint.constant) / selectedPhotoScrollView.zoomScale
    width = photosContainerView.frame.width / selectedPhotoScrollView.zoomScale
    height = (photosContainerView.frame.height - 2 * landscapeCropHeightCostraint.constant) / selectedPhotoScrollView.zoomScale
}
else {
    originX = (selectedPhotoScrollView.contentOffset.x + portraitCropWidthConstraint.constant) / selectedPhotoScrollView.zoomScale
    originY = selectedPhotoScrollView.contentOffset.y / selectedPhotoScrollView.zoomScale
    width = (selectedPhotoScrollView.frame.width - 2 * portraitCropWidthConstraint.constant) / selectedPhotoScrollView.zoomScale
    height = selectedPhotoScrollView.frame.height / selectedPhotoScrollView.zoomScale
}

let rotatedPoint = CGPoint(x: originX, y: originY).applying(CGAffineTransform(rotationAngle: lastRotation))

if let croppedImage = image.cropedToRect(rect: CGRect(x: rotatedPoint.x, y: rotatedPoint.y, width: width, height: height)) {
    delegate?.photoPicker(photoPicker: self, didSelectPhoto: croppedImage)
}

Problem start when I rotate an image. Can someone show me a sample code?

Kamil Harasimowicz
  • 4,684
  • 5
  • 32
  • 58

0 Answers0