GithubHelp home page GithubHelp logo

zimagecropper's Introduction

ZImageCropper(Swift 4)

ZImageCropper is a simplest way to crop image to any shapes you like.

alt ZImageCropper

Behind the scene

ZImageCropper is using following things as core part:

  • UIBezierPath
  • UITouch Events
  • CAShapeLayer
  • CoreGraphics

How to use it?

Crop using AI (Or programmatically)

Try Following steps,

Step 1: Add ZImageCropper.swift file to your project.

Step 2: Crop image using following,

let croppedImage = ZImageCropper.cropImage(ofImageView: yourImageView, withinPoints: [
CGPoint(x: 0, y: 0),   //Start point
CGPoint(x: 100, y: 0),
CGPoint(x: 100, y: 100),
CGPoint(x: 0, y: 100)  //End point
])

Note : Make sure you provide valid points, points must be >=2 in count.

Crop using user touches

According to given demo project do as following in your project.

  • Create IBOutlet of your image that you want to crop. (or add one UIImageView object to your class)
    //Your UIImageView object whose image will be cropped
    @IBOutlet weak var tempImageView: UIImageView!
  • Add following code after class declaration:
    //Update this for path line color
    let strokeColor:UIColor = UIColor.blue
    
    //Update this for path line width
    let lineWidth:CGFloat = 2.0
    
    //Path to draw while touch events occur
    var path = UIBezierPath()
    
    //ShapeLayer of cropped view
    var shapeLayer = CAShapeLayer()
    
    //Final Cropped Image
    var croppedImage = UIImage()
  • Add UITouch even methods like this:
 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first as UITouch?{
            let touchPoint = touch.location(in: self.tempImageView)
            print("touch begin to : \(touchPoint)")
            path.move(to: touchPoint)
        }
    }
    
    override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first as UITouch?{
            let touchPoint = touch.location(in: self.tempImageView)
            print("touch moved to : \(touchPoint)")
            path.addLine(to: touchPoint)
            addNewPathToImage()
        }
    }
    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first as UITouch?{
            let touchPoint = touch.location(in: self.tempImageView)
            print("touch ended at : \(touchPoint)")
            path.addLine(to: touchPoint)
            addNewPathToImage()
            path.close()
        }
    }
    
    override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent?) {
        if let touch = touches.first as UITouch?{
            let touchPoint = touch.location(in: self.tempImageView)
            print("touch canceled at : \(touchPoint)")
            path.close()
        }
    }
  • Add additional methods:
 /**
    This methods is adding CAShapeLayer line to tempImageView
    */
    func addNewPathToImage(){
        shapeLayer.path = path.cgPath
        shapeLayer.strokeColor = strokeColor.cgColor
        shapeLayer.fillColor = UIColor.clear.cgColor
        shapeLayer.lineWidth = lineWidth
        tempImageView.layer.addSublayer(shapeLayer)
    }
    
    /**
    This methods is making cropped object of tempImageView's image
    */
    func cropImage(){
        UIGraphicsBeginImageContextWithOptions(tempImageView.bounds.size, false, 1)
        
        tempImageView.layer.render(in: UIGraphicsGetCurrentContext()!)
        
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        
        UIGraphicsEndImageContext()
        
        self.croppedImage = newImage!
    }
  • Add two methods for croping and cancelling crop:
    @IBAction func IBActionCropImage(_ sender: UIButton) {
        shapeLayer.fillColor = UIColor.black.cgColor
        tempImageView.layer.mask = shapeLayer
    }
    
    @IBAction func IBActionCancelCrop(_ sender: UIButton) {
        shapeLayer.removeFromSuperlayer()
        path = UIBezierPath()
        shapeLayer = CAShapeLayer()
    }

Feel free to ask any help! Twitter: @ZaidKhanIntel

zimagecropper's People

Contributors

zaidpathan avatar zaidsa avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.