TOCropViewController

repository·main·Indexed 26 days ago

https://github.com/timoliver/tocropviewcontroller

An open-source UIViewController subclass for cropping images and performing basic rotations, modeled after the iOS Photos app editor. Supports circular cropping, custom zoom animations, and integration with UIActivityViewController for sharing. Available for Swift and Objective-C projects.

Tokens
2.1K
Snippets
5
Records
6
Agent score
39%

What's inside TOCropViewController

  1. Create a circular cropped image

    main

    You can configure the controller to crop circular copies of images by setting the croppingStyle to .circular in Swift or TOCropViewCroppingStyleCircular in Objective-C. Use the corresponding circular delegate methods to retrieve the result.

    // 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
    }
  2. Implement basic image cropping in Objective-C

    main

    To perform a basic crop in Objective-C, initialize TOCropViewController with a UIImage, set its delegate, and present it modally. Implement the cropViewController:didCropToImage:withRect:angle: delegate method to receive the resulting image.

    - (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
    }
  3. Present with a custom zoom animation

    main

    For a custom presentation where an existing image view zooms in to fill the screen, use the presentAnimated(fromParentViewController:fromFrame:completion:) method in Swift or presentAnimatedFromParentViewController:fromFrame:completion: in Objective-C.

    // 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];
    }
  4. Implement basic image cropping in Swift

    main

    To perform a basic crop in Swift, initialize CropViewController with a UIImage, set its delegate, and present it modally. Implement the cropViewController(_:didCropToImage:withRect:angle:) delegate method to receive the resulting image.

    func presentCropViewController() {
      let image: UIImage = ... //Load an image
      
      let cropViewController = CropViewController(image: image)
      cropViewController.delegate = self
      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
    }
  5. Share cropped images via UIActivityViewController

    main

    To automatically present a share sheet (UIActivityViewController) immediately after the user finishes cropping, set showActivitySheetOnDone to true (Swift) or YES (Objective-C).

    // Swift
    let cropViewController = CropViewController(image: image)
    cropViewController.showActivitySheetOnDone = true
    // Objective-C
    TOCropViewController *cropViewController = [[TOCropViewController alloc] initWithImage:image];
    cropViewController.showActivitySheetOnDone = YES;