0

I am trying to make a simple Crop functionality with Swift. I am trying with CGImageCreateWithImageInRect function - which works perfectly but produce inferior quality. Am I missing something ?

 func retriveCroppedImage(){
    let yratio: CGFloat = imgviewrect.size.height / chosenImage.size.height
    let xratio: CGFloat = imgviewrect.size.width / chosenImage.size.width

    var cliprect = CGRectMake(centerpoint.x - vWidth/2, centerpoint.y - vHeight/2, vWidth, vHeight)
    print("cliprect top  \(cliprect.size)")
    cliprect.size.height =  cliprect.size.height / xratio;
    cliprect.size.width =  cliprect.size.width / xratio;
    cliprect.origin.x = cliprect.origin.x / xratio + imgviewrect.origin.x  / xratio
    cliprect.origin.y = cliprect.origin.y / yratio - imgviewrect.origin.y  / xratio

    print("cliprect  On Image \(cliprect)")

    let imageRef =  CGImageCreateWithImageInRect(chosenImage.CGImage, cliprect )

    croppedImg  = UIImage(CGImage: imageRef!, scale:  UIScreen.mainScreen().scale, orientation: chosenImage.imageOrientation)
    print("Operation complete");
}

Screen shots : Main VC

after cropping I get Cropped Image

Deb S
  • 509
  • 5
  • 16
  • It's hard to tell what else might be happening in your screenshots - the gray box is in the way. Use `UIImagePNGRepresentation` and `NSData.writeToFile()` to get the original and cropped images, and give us links to those instead. – Kurt Revis Jan 02 '16 at 20:24
  • @KurtRevis The gray box is just a custom view - which is floating and can be panned or zoomed. Just get the area of crop. It's not drawn on top of of image. Thank you of the suggestion - will try your option. – Deb S Jan 02 '16 at 20:38
  • http://stackoverflow.com/a/33091111/2303865 – Leo Dabus Jan 02 '16 at 21:06

1 Answers1

0

After trying all the options - I found accidentally I set Alpha in Image View on the story board. There was nothing wrong with the CGImageCreateWithImageInRect function. Now my cropping app is working as desired. But thank you all for the suggestions.

Deb S
  • 509
  • 5
  • 16