-2
UIGraphicsBeginImageContextWithOptions(startingImageL.size, false, 0.0) 
    let context = UIGraphicsGetCurrentContext()!

    startingImageL.draw(in: CGRect(origin: CGPoint.zero, size: startingImageL.size), blendMode: .copy, alpha: 1.0)

    context.setBlendMode(.copy)
    context.setFillColor(UIColor.clear.cgColor)

    let rectPath = UIBezierPath(rect:  CGRect(origin: CGPoint.zero, size: startingImageL.size))
    let cropRectPath = UIBezierPath(rect: cropZone)

    print("cropRectPath \(cropRectPath.debugDescription) ")
    print("cropZone \(cropZone.debugDescription) ")

    print("cropZone width \(cropZone.width) ")
    print("cropZone height \(cropZone.height) ")

    print("cropZone maxX \(cropZone.maxX) ")
    print("cropZone maxY \(cropZone.maxY) ")


    rectPath.append(cropRectPath)
    rectPath.usesEvenOddFillRule = true

    context.saveGState()
    rectPath.fill()
    context.clip(to: cropZone)
//        UIRectClip(cropZone) <-nope
//        UIRectFrame(cropZone) <- nope
//        rectPath.addClip() <nope

before image

cropping zone after image

It crops great but I want it to delete all the space around it instead of fill (the pink) and then the image would fill up the view. Thanks!

devjme
  • 684
  • 6
  • 12
  • "It crops great" No it doesn't. What you are doing is not cropping at all. See my answer to this question: https://stackoverflow.com/questions/43720720/how-to-crop-a-uiimageview-to-a-new-uiimage-in-aspect-fill-mode – matt Jun 19 '18 at 01:18
  • 1
    And this one (might be simpler and easier to grasp the basics of what cropping really is): https://stackoverflow.com/questions/40792409/cropping-ciimage – matt Jun 19 '18 at 01:19
  • hmmm ok let me work with it and get back to you :D – devjme Jun 19 '18 at 03:01
  • Anymore help is welcome btw :) i already saw Matt's post before I posted which helped me... but I guess i missed the direction lol :D – devjme Jun 19 '18 at 03:02
  • @matt YESSS you saved me I never thought about starting it off screen!! THANK YOU – devjme Jun 19 '18 at 09:51

1 Answers1

0

Per Matt's advice all I needed was to push to negative coordinates and start the image there!! So all I needed (once you have the crop rect) is this:

    UIGraphicsBeginImageContextWithOptions(cropZone.size, false, 0.0)
    UIGraphicsGetCurrentContext()

    startingImageL.draw(at: CGPoint(x:-cropZone.origin.x,
                                     y:-cropZone.origin.y))
    let result = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

Complete answer here and his project is here

THANK YOU

devjme
  • 684
  • 6
  • 12