Giter Site home page Giter Site logo

asokeios's Introduction

Asoke

View controller for cropping profile pictures into rectangles, squares, and circles

Asoke is a dangerously simple way to add a view controller to your iOS Swift app that acts to crop photos to either a circle or a sqaure/rectangle. You can change the circle radius or the rect cropping area.

Smiley face
Smiley face

Documentation

Integration into your project:

  1. Download the source files. Drag and drop them into your project. None of that Cocoapods BS. They are currently Swift 3.1 and Xcode 8.2 compatible.
  2. See number 1.

Presenting the view controller:

The view controller is simple to present. The following is an example for presenting the rectangualar crop VC:
    // You will need to init with an image and a cropping rect
    guard let image = UIImage(named: "yourImage") else { return }
    let rect = CGRect(x: 10, y: 150, width: 300, height: 240)
    let rectVC = AsokeRectangularVC(croppingRect: rect, withImageToCrop: image)
    
    // set the delegate and present.
    rectVC.delegate = self
    self.present(rectVC, animated: true, completion: nil)

And the following is an example for presenting the circular crop VC:

    // Need to init with an image and a radius
    guard let image = UIImage(named: "me") else { return }
    let circleVC = AsokeCircularVC(circleRadius: 158, withImageToCrop: image)
    circleVC.delegate = self
    self.present(circleVC, animated: true, completion: nil)

A few things to note:

  1. For the rectVC (the view controller that crops to a rectangle/square), the origin of your cropping rect may not: 1) be less than 0, and 2) the width of the cropping rect may not exceed the width of the viewcontroller's view property.
  2. For the circular view controller option, the radius may not exceed ((viewController.view.width / 2) - (1.5)). For example, on an iPhone 5, the view controller width property is 320, so the radius for your cropping circle may not exceed 158.5

Delegate callbacks

There are callbacks to your initial view controller for when certain actions occur: pressing the cancel button, the save button, or when an error occurs. You must implement all 3 to properly conform to the delegate.

  extension ViewController: AsokeDelegate {
   func cancelButtonWasPressed() {
        // cancel here, dismiss the VC, etc. This is called when the cancel button is pressed
   }

   func didFailWithError(_ error: NSError) {
        // handle the error here
    }

    func didFinishCroppingImage(_ croppedImage: UIImage) {
        // do something here with your image
   }
}

Integration with UIImagePickerController

Many people look for a more customizable verion of editor for UIImagePickerController. They have a set default cropping rect that may not be suitable for all apps. Actually, that's what inspired this library. Asoke integrates well with UIImagePickerController, allowing you to choose a different cropping rect/circle for the image you chose from UIImagePickerController. Here is an example:

   func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    // make sure there is an image before seguing
    if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
       // Need to init with an image and a radius
         let circleVC = AsokeCircularVC(circleRadius: 158, withImageToCrop: pickedImage)
         circleVC.delegate = self
         picker.show(circleVC, sender: self)
    }
}

asokeios's People

Contributors

joeybodnar 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.