Search code examples
swiftcgrect

How can I use CGRect() dynamically?


I have the following scene example where I can crop an image based on the selection (red square).

That square has dynamic Height and Width - base on this fact I want to use the selected Height and Width to crop what is inside of the Red square.

The function that I am using for cropping is from Apple developer and looks like this:

func cropImage(_ inputImage: UIImage, toRect cropRect: CGRect, viewWidth: CGFloat, viewHeight: CGFloat) -> UIImage? 
{    
    let imageViewScale = max(inputImage.size.width / viewWidth,
                             inputImage.size.height / viewHeight)

    // Scale cropRect to handle images larger than shown-on-screen size
    let cropZone = CGRect(x:cropRect.origin.x * imageViewScale,
                          y:cropRect.origin.y * imageViewScale,
                          width:cropRect.size.width * imageViewScale,
                          height:cropRect.size.height * imageViewScale)

    // Perform cropping in Core Graphics
    guard let cutImageRef: CGImage = inputImage.cgImage?.cropping(to:cropZone)
    else {
        return nil
    }

    // Return image to UIImage
    let croppedImage: UIImage = UIImage(cgImage: cutImageRef)
    return croppedImage
}

Now. I want to use the given Height and Width to crop that selection.

 let croppedImage =  cropImage(image!, toRect: CGRect(x:?? , y:?? , width: ??, height: ??), viewWidth: ??, viewHeight: ??) 

What should I fill in these parameters in order to crop the image based on the above dynamic selection?


Solution

  • I've used the info from all of your answers and especially @matt's comment and this is the final solution.

    Using the input values that my red square returned, I've adapted the original Crop function to this one:

        func cropImage(_ inputImage: UIImage, width: Double, height: Double) -> UIImage?
        {
    
            let imsize = inputImage.size
            let ivsize = UIScreen.main.bounds.size
            
            var scale : CGFloat = ivsize.width / imsize.width
            if imsize.height * scale < ivsize.height {
                scale = ivsize.height / imsize.height
            }
    
            let croppedImsize = CGSize(width:height/scale, height:width/scale)
    
            
            let croppedImrect =
                CGRect(origin: CGPoint(x: (imsize.width-croppedImsize.width)/2.0,
                                       y: (imsize.height-croppedImsize.height)/2.4),
                       size: croppedImsize)
            
            let r = UIGraphicsImageRenderer(size:croppedImsize)
            let croppedIm = r.image { _ in
                inputImage.draw(at: CGPoint(x:-croppedImrect.origin.x, y:-croppedImrect.origin.y))
            }
            
            return croppedIm
            
        }