• Stars
    star
    4,571
  • Rank 8,898 (Top 0.2 %)
  • Language
    Objective-C
  • License
    MIT License
  • Created almost 9 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A view controller for iOS that allows users to crop portions of UIImage objects

TOCropViewController

CI Version Carthage compatible GitHub license Platform

TOCropViewController is an open-source UIViewController subclass to crop out sections of UIImage objects, as well as perform basic rotations. It is excellent for things like editing profile pictures, or sharing parts of a photo online. It has been designed with the iOS Photos app editor in mind, and as such, behaves in a way that should already feel familiar to users of iOS.

For Swift developers, CropViewController is a Swift wrapper that completely encapsulates TOCropViewController and provides a much more native, Swiftier interface.

Proudly powering apps by

Looking for something more? If TOCropViewController doesn't meet your exact requirements, please consider IMG.LY with video editing and photo filter capabilities instead! (Disclaimer: Affiliate Link)

Features

  • Crop images by dragging the edges of a grid overlay.
  • Optionally, crop circular copies of images.
  • Rotate images in 90-degree segments.
  • Clamp the crop box to a specific aspect ratio.
  • A reset button to completely undo all changes.
  • iOS 7/8 translucency to make it easier to view the cropped region.
  • The choice of having the controller return the cropped image to a delegate, or immediately pass it to a UIActivityViewController.
  • A custom animation and layout when the device is rotated to landscape mode.
  • Custom 'opening' and 'dismissal' animations.
  • Localized in 28 languages.

System Requirements

iOS 11.0 or above

Installation

CocoaPods

Objective-C

Add the following to your Podfile:

pod 'TOCropViewController'

Swift

Add the following to your Podfile:

pod 'CropViewController'
Swift Package Manager

Add the following to your Package.swift:

dependencies: [
  // ...
  .package(url: "https://github.com/TimOliver/TOCropViewController.git"),
],
Carthage
  1. Add the following to your Cartfile:
github "TimOliver/TOCropViewController"
  1. Run carthage update

  2. From the Carthage/Build folder, import one of the two frameworks into your Xcode project. For Objective-C projects, import just TOCropViewController.framework and for Swift, import CropViewController.framework instead. Each framework is separate; you do not need to import both.

  3. Follow the remaining steps on Getting Started with Carthage to finish integrating the framework.

Manual Installation

All of the necessary source and resource files for TOCropViewController are in Objective-C/TOCropViewController, and all of the necessary Swift files are in Swift/CropViewController.

For Objective-C projects, copy just the TOCropViewController directory to your Xcode project. For Swift projects, copy both TOCropViewController and CropViewController to your project.

Examples

Using TOCropViewController is very straightforward. Simply create a new instance passing the UIImage object you wish to crop, and then present it modally on the screen.

While TOCropViewController prefers to be presented modally, it can also be pushed to a UINavigationController stack.

For a complete working example, check out the sample apps included in this repo.

Basic Implementation

Swift

func presentCropViewController() {
  let image: UIImage = ... //Load an image
  
  let cropViewController = CropViewController(image: image)
  cropViewController.delegate = self
  present(cropViewController, animated: true, completion: nil)
}

func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
        // 'image' is the newly cropped version of the original image
    }

Objective-C

- (void)presentCropViewController
{
  UIImage *image = ...; // Load an image
  
  TOCropViewController *cropViewController = [[TOCropViewController alloc] initWithImage:image];
  cropViewController.delegate = self;
  [self presentViewController:cropViewController animated:YES completion:nil];
}

- (void)cropViewController:(TOCropViewController *)cropViewController didCropToImage:(UIImage *)image withRect:(CGRect)cropRect angle:(NSInteger)angle
{
  // 'image' is the newly cropped version of the original image
}

Similar to many UIKit UIViewController subclasses, like MFMailComposeViewController, the class responsible for presenting view controller should also take care of dismissing it upon cancellation. To dismiss TOCropViewController, implement the cropViewController:didFinishCancelled: delegate method, and call dismissViewController:animated: from there.

Making a Circular Cropped Image

Swift

func presentCropViewController() {
    var image: UIImage? // Load an image
    let cropViewController = CropViewController(croppingStyle: .circular, image: image)
    cropViewController.delegate = self
    self.present(cropViewController, animated: true, completion: nil)
}

func cropViewController(_ cropViewController: TOCropViewController?, didCropToCircularImage image: UIImage?, with cropRect: CGRect, angle: Int) {
    // 'image' is the newly cropped, circular version of the original image
}

Objective-C

- (void)presentCropViewController
{
UIImage *image = ...; // Load an image

TOCropViewController *cropViewController = [[TOCropViewController alloc] initWithCroppingStyle:TOCropViewCroppingStyleCircular image:image];
cropViewController.delegate = self;
[self presentViewController:cropViewController animated:YES completion:nil];
}

- (void)cropViewController:(TOCropViewController *)cropViewController didCropToCircularImage:(UIImage *)image withRect:(CGRect)cropRect angle:(NSInteger)angle
{
// 'image' is the newly cropped, circular version of the original image
}
Sharing Cropped Images Via a Share Sheet

Swift

func presentCropViewController() {
    var image: UIImage? // Load an image
    let cropViewController = CropViewController(image: image)
    cropViewController.showActivitySheetOnDone = true
    self.present(cropViewController, animated: true, completion: nil)
}

Objective-C

- (void)presentCropViewController
{
  UIImage *image = ...; // Load an image
  
  TOCropViewController *cropViewController = [[TOCropViewController alloc] initWithImage:image];
  cropViewController.showActivitySheetOnDone = YES;
  [self presentViewController:cropViewController animated:YES completion:nil];
}
Presenting With a Custom Animation

Optionally, TOCropViewController also supports a custom presentation animation where an already-visible copy of the image will zoom in to fill the screen.

Swift

func presentCropViewController() {
    var image: UIImage? // Load an image
    var imageView = UIImageView(image: image)
    var frame: CGRect = view.convert(imageView.frame, to: view)
    
    let cropViewController = CropViewController(image: image)
    cropViewController.delegate = self
    self.present(cropViewController, animated: true, completion: nil)
    cropViewController.presentAnimated(fromParentViewController: self, fromFrame: frame, completion: nil)
}

Objective-C

- (void)presentCropViewController
{
  UIImage *image = ...;
  UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
  CGRect frame = [self.view convertRect:imageView.frame toView:self.view];
  
  TOCropViewController *cropViewController = [[TOCropViewController alloc] initWithImage:image];
  cropViewController.delegate = self;
  [self presentViewController:cropViewController animated:YES completion:nil];
  [cropViewController presentAnimatedFromParentViewController:self fromFrame:frame completion:nil];
}

Architecture of TOCropViewController

While traditional cropping UI implementations will usually just have a dimming view with a square hole cut out of the middle, TOCropViewController goes about its implementation a little differently.

Since there are two views that are overlaid over the image (A dimming view and a translucency view), trying to cut a hole open in both of them would be rather complex. Instead, an image view is placed in a scroll view in the background, and a copy of the image view is placed on top, inside a container view that is clipped to the designated cropping size. The size and position of the foreground image is then made to match the background view, creating the illusion that there is a hole in the dimming views, and minimising the number of views onscreen.

Credits

TOCropViewController was originally created by Tim Oliver as a component for iComics, a comic reader app for iOS.

Thanks also goes to TOCropViewController's growing list of contributors!

iOS Device mockups used in the screenshot created by Pixeden.

License

TOCropViewController is licensed under the MIT License, please see the LICENSE file.

More Repositories

1

TOWebViewController

A view controller class for iOS that allows users to view web pages directly within an app.
Objective-C
1,490
star
2

TORoundedButton

A high-performance button control with rounded corners for iOS.
Objective-C
481
star
3

PassKit-Business-Card

A template for iOS Wallet passes that can be used like business cards.
Objective-C
387
star
4

TOPasscodeViewController

A modal passcode input and validation view controller for iOS
Objective-C
383
star
5

TONavigationBar

Replicating the 'clear' navigation bar style of the iOS 12 Apple TV app.
Objective-C
248
star
6

TOStatusBarSimulator

Replaces the iOS system status bar with a configurable mockup for the purpose of marketing screenshots.
Objective-C
199
star
7

TOSegmentedControl

A segmented control in the style of iOS 13 compatible with previous versions of iOS.
Objective-C
195
star
8

TOGlintyStringView

A non-App Store safe re-implementation of the 'slide to unlock' visual effect on iOS.
Objective-C
186
star
9

TOSMBClient

An Objective-C binding around the libDSM SMB client library.
Objective-C
177
star
10

TOActionSheet

A custom-designed reimplementation of the UIActionSheet control for iOS
Objective-C
173
star
11

TORoundedTableView

A subclass of UITableView that styles it like Settings.app on iPad
Objective-C
162
star
12

RealmBrowser-iOS

A native iOS debugging framework for introspecting Realm files on device
Objective-C
148
star
13

TOSearchBar

A basic reimplementation of UISearchBar with easier theming, and nicer animation.
Objective-C
104
star
14

TOScrollBar

An interactive scroll bar for traversing comically massive scroll views.
Objective-C
91
star
15

PHP-Framework-Classes

A collection of PHP classes I've developed for personal projects.
PHP
83
star
16

TOInsetGroupedTableView

An iOS 11 back-port of the grouped inset table view style in iOS 13.
Objective-C
79
star
17

TODocumentPickerViewController

An open-source implementation for picking files/folders out of an online API
Objective-C
78
star
18

WebPKit

A framework that extends a variety of Cocoa APIs with capabilities for encoding and decoding WebP files for all of Apple's platforms.
Swift
67
star
19

TOSplitViewController

A split view controller that can display up to three view controllers at once.
Objective-C
60
star
20

Beeline

An extremely lean implementation on the classic iOS router pattern.
Swift
59
star
21

TOFileSystemObserver

A bullet-proof mechanism for detecting any changes made to the contents of a folder in iOS and macOS.
Objective-C
38
star
22

TOAlertViewController

A modern looking modal popup UI component for iOS and iPadOS.
Objective-C
24
star
23

TOAppSettings

An object wrapper for storing properties in NSUserDefaults.
Objective-C
24
star
24

TOAppRater

A very small library to provide a classier way of requesting app reviews.
Objective-C
21
star
25

TOReachability

A lightweight, unit-tested class that detects network status changes on iOS.
Objective-C
20
star
26

TOBrowserActivityKit

A set of UIActivity subclasses for opening NSURL objects in Chrome or Safari.
Objective-C
20
star
27

WebP-Cocoa

An unofficial collection of precompiled WebP binaries for all of Apple's current platforms.
Shell
18
star
28

BuildingHighPerformanceListsAndCollectionViews

A mirror of Apple's sample code for high performance collection views in iOS 15.
Swift
18
star
29

TOBadgeView

A badge view that can be used to accessorize other UI elements in iOS.
Objective-C
16
star
30

TOPagingView

A paging scroll view that can handle arbitrary numbers of page views at run-time.
Objective-C
14
star
31

TOBorderView

A flexible container view featuring a solid background with rounded corners.
Objective-C
14
star
32

TOGridView

A lightweight collection view for iOS
Objective-C
13
star
33

TORevealViewController

An alternative to UISplitViewController on both iPhone and iPad
Objective-C
11
star
34

TORoundedWindow

A UIWindow overlay that adds rounded corners to an iOS app display region
Objective-C
10
star
35

TOSegmentedTabBarController

A splittable tab bar controller controlled by a segmented control view.
Objective-C
9
star
36

TOWebContentViewController

A view controller to quickly render arbitrary local HTML content.
Objective-C
9
star
37

TODocumentsDirectoryWatcher

Automatically observes an iOS app's Documents directory for file changes.
Objective-C
8
star
38

TOPagerView

A UIScrollView subclass that allows paged horizontal swiping with a re-use mechansim similar to UITableView.
Objective-C
8
star
39

TOSegmentedSplitViewController

A split view controller that is controlled by a segmented control on small devices
Objective-C
8
star
40

TOFileKit

Not ambitious at all.
Objective-C
7
star
41

AVRecorder

A mirror of the AVRecorder sample app for macOS by Apple
Objective-C
7
star
42

TOTabBarController

A tab bar controller that mimics the vertical tool bar style of Tweetbot 4.
Objective-C
7
star
43

PhotoSorter

A quick and dirty CLI tool for sorting my photo library
Swift
7
star
44

YozoraRedux

My personal color theme I use in Xcode. Tweaked from Yozora and Sublime Text.
7
star
45

LargeImageDownsizing

Mirror of Apple's sample code for partially decoding regions of images into memory
Objective-C
6
star
46

RLMFastImage

A prototype for storing raw pixel data in Realm.
Objective-C
6
star
47

WebServerKit

A fork of the #1 HTTP server for iOS, macOS & tvOS
Objective-C
5
star
48

BTLE-Transfer

A mirror of the Apple sample code that demonstrates how to work with Core Bluetooth
Objective-C
5
star
49

TOFileAttributes

A Realm-like dynamic property wrapper that maps to APFS extended file attributes.
Objective-C
5
star
50

Spackle

A collection of convenience properties and extensions for laying out views in UIKit
Swift
5
star
51

UIColor-LegacySemanticColors

A UIColor category that backports the new semantic colors of iOS 13 to iOS 12.
Objective-C
5
star
52

SLVolumeButtonListener

A class for iOS that lets apps intercept events from the device's volume buttons
Objective-C
4
star
53

TimOliver

My GitHub README.md repository. Feel free to copy and re-use this as a template for your own GitHub account! 😁
4
star
54

TOLocalServiceDiscovery

A wrapper for detecting accessible devices locally via Bonjour.
Objective-C
4
star
55

AnimeLabPiP

A Safari Extension to enable Picture-in-Picture with AnimeLab's web player.
JavaScript
4
star
56

TOTARArchive

A basic library to allow streaming data out of TAR files
Objective-C
3
star
57

TOStackView

A barebones container view for UIKit that stacks collections of subviews
Objective-C
3
star
58

icomics.co

The source code to the official iComics website.
PHP
3
star
59

CI

A central repo for the build scripts used for testing and deploying my projects
Ruby
3
star
60

TOPropertyAccessor

Swift Property Wrappers, but in Objective-C. And done horribly.
Objective-C
3
star
61

simple-iphone-image-processing

Automatically exported from code.google.com/p/simple-iphone-image-processing
Objective-C++
2
star
62

CloudKit-PHP

For server-to-server comms from PHP to CloudKit.
PHP
2
star
63

UIDevice-UserInterfaceExtendedIdiom

An Objective-C category to allow easier detection of newer iOS device UI idioms (such as iPhone 5).
Objective-C
2
star
64

vCard

The source code to my online vCard site.
PHP
2
star
65

Dexter

An extensible encyclopedia engine
1
star
66

rchat

Swift
1
star
67

SteamAppCatalog

A dump of all of the JSON files extracted from Steam's store
1
star
68

SteamLibraryDatabase

A small utility to copy the entire Steam game library to a Realm database.
Objective-C
1
star
69

ObjC-WebPImage

An Objective-C framework for encoding and decoding WebP image files
Objective-C
1
star