Home > iOS Development > SSPhotoCropperViewController: A custom ViewController that provides UI for cropping photos in iOS apps

SSPhotoCropperViewController: A custom ViewController that provides UI for cropping photos in iOS apps

We all sometimes do something with photos in our apps. And apps that deal with photos in some way, whether it be editing, stylishing or sharing them, are becoming more and more popular every day.

In one of my apps I was recently working on, I needed a simple UI to crop a picture into a square shape. So I wrote SSPhotoCropperViewController. It’s a custom view controller that provides a simple, configurable and easy-to-use UI for cropping and scaling photos in iPhone & iPod Touch apps.

 

Add SSPhotoCropperViewController to Your Project

 

Implement SSPhotoCropperDelegate

In the view controller that you will show SSPhotoCropperViewController, implement SSPhotoCropperDelegate methods to interact with SSPhotoCropperViewController.

 

Create and Show SSPhotoCropperViewController

  • Create an SSPhotoCropperViewController object instance by using one of the -init methods provided, set the source photo, UI mode etc.
  • Configure it by modifying some properties before showing the view controller.
  • Show it by presenting it as a modal view controller or by pushing it on to a navigation controller.

 

Notes

  • The demo project on GitHub includes a simple example on how to use SSPhotoCropperViewController. Have a look at it if you encounter a problem using SSPhotoCropperViewController in your project.
  • Questions, suggestions, bug-reports, github forks & any other contributions are welcome. Don’t hesitate to contact me at @ardalahmet or via ardalahmet(at)gmail.com.
  • The source code is licensed under the Apache License, Version 2.0. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  • You can modify the source code to make it meet your needs. You may want to change the crop square size or crop photos into rectangle shapes etc.

 

Screenshots

Photo Cropper

Photo Cropper

After Cropping the Photo

After Cropping the Photo

  1. anil
    November 16th, 2011 at 12:44 | #1

    Hey Ardal

    it was a very nice code. But there is one thing i want to ask

    you set the size of image view to half CGRect imageViewFrame = CGRectMake(0.0f, 0.0f, roundf(w /2.0f), roundf(h / 2.0f)); in loadPhoto method.

    and while croping there is
    CGFloat cw = 300.0f / zoomScale;
    CGFloat ch = 300.0f / zoomScale;

    i can figure out how you calculated 300 number.

    i want to show image on full screen i.e. 320 * 480 and so the size will be same as this. How will i achieve this.
    thanks,

  2. November 20th, 2011 at 22:56 | #2

    Hi @anil

    What do you exactly want to display full screen? The image view or the scroll view that holds the image view?
    Nothing should be changed other than the boundaries of the scroll view. And the code doesn’t depend on any constants related to the size of the scroll view, all scroll view related dimensions in the code are being freshly taken from the scroll view itself. You can just resize the scroll view in the IB and check whether it works fine or not. However if you want to change the size of the crop rectangle, then you’ll need to make some adjustments in the dragging and cropping code according to the new rectangle size. Hope this helps.

  3. Richy
    December 6th, 2011 at 16:51 | #3

    Very nice tutorial. My question is , can we set the dynamic size of the crop rectangle according to user touches so that user can select whatever image portion as per requirement? Thanks.

  4. December 6th, 2011 at 20:08 | #4

    Hi @Richy
    The user can zoom in and out the image to adjust the area of the image that she wants to crop. Making the crop rectangle fixed makes things simple, so I chose to design it so. However, you can always modify the code to make the crop rectangle resizable.
    Thanks, hope this helps.

  5. Richy
    December 7th, 2011 at 09:58 | #5

    @Ahmet : Thanks for the quick reply. I will try this .

  6. Matthew Walk
    February 9th, 2012 at 08:17 | #6

    Hi Ahmet,

    This is a wonderful tutorial! Would it be easy to change the shape of the crop from rectangle to an oval shape (closer to the shape of the human head)?

  7. February 10th, 2012 at 11:32 | #7

    Hi @Matthew,
    I’m not sure but I don’t think that it’s too easy to do cropping with an oval shape. I suppose, it requires some image processing.

  1. October 21st, 2011 at 11:44 | #1
  2. October 24th, 2011 at 04:19 | #2
  3. January 10th, 2012 at 10:36 | #3