TimOliver / TOCropViewController

A view controller for iOS that allows users to crop portions of UIImage objects
http://www.timoliver.com.au/2015/06/21/tocropviewcontroller-an-open-source-image-cropper-for-ios/
MIT License
4.67k stars 929 forks source link

[UIImage croppedImageWithFrame:angle:circularClip:]: unrecognized selector sent to instance #585

Closed madhavgharmalkar closed 2 weeks ago

madhavgharmalkar commented 2 weeks ago

Describe the bug A clear and concise description of what the bug is.

I am initializing a CropViewController with a UIImage. However when I press "done" in the presented ViewController I'm receiving the following error. I do not receive this error when the delegate is nil.

"-[UIImage croppedImageWithFrame:angle:circularClip:]: unrecognized selector sent to instance 0x600003002e20"

To Reproduce Steps to reproduce the behavior:

import CropViewController
import SwiftUI

public struct CropImage: UIViewControllerRepresentable {
  public var image: UIImage

  public init(image: UIImage) {
    self.image = image
  }

  public func makeUIViewController(context: Context) -> CropViewController {
    let cropViewController = CropViewController(image: image)
    cropViewController.delegate = context.coordinator
    return cropViewController
  }

  public func updateUIViewController(_ uiViewController: CropViewController, context: Context) {

  }

  public typealias UIViewControllerType = CropViewController

  // MARK: Coordinator
  public class Coordinator: NSObject, CropViewControllerDelegate {
    var parent: CropImage

    init(parent: CropImage) {
      self.parent = parent
    }

    public func cropViewController(
      _ cropViewController: CropViewController, didCropToImage image: UIImage,
      withRect cropRect: CGRect, angle: Int
    ) {
      parent.image = image
    }
  }

  public func makeCoordinator() -> Coordinator {
    return Coordinator(parent: self)
  }
}

Expected behavior A clear and concise description of what you expected to happen.

I should expect the delegate to be called.

Screenshots If applicable, add screenshots to help explain your problem.

iOS Device:

Additional context

Given an UIImage image, I am able to see image.croppedImage(withFrame: , angle: , circularClip:)

madhavgharmalkar commented 2 weeks ago

I was able to find a solution, and I'm sharing what I did to get it to work here. I am using Tuist + SwiftUI.

For some reason, it seems that when using CropViewController (the swift version), Xcode is able to find the header declaration for UIImage+CropView.h but unable to find the implementation for it. My solution was to use the ObjC VC TOCropVuewController and include the headers via settings.

settings: .settings(
      base: [
        "HEADER_SEARCH_PATHS": [
          "$(SRCROOT)/Tuist/.build/checkouts/TOCropViewController/Objective-C/TOCropViewController/include/TOCropViewController"
        ]
      ]
    )