Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwiftUI example of using TOCropViewController #421

Open
ClaesClaes opened this issue Jul 14, 2020 · 7 comments
Open

SwiftUI example of using TOCropViewController #421

ClaesClaes opened this issue Jul 14, 2020 · 7 comments

Comments

@ClaesClaes
Copy link

Hi, are there a SwiftUI example of using TOCropViewController? I've just started to learn SwiftUI and have no previous experiences of UIViewcontroller so would help get me going.

@TimOliver
Copy link
Owner

TimOliver commented Jul 20, 2020

Hi @ClaesClaes! Oh wow! Welcome to the Swift community then!

A few people have asked this before. Apple's released a few tutorials that hopefully point in the right direction: https://developer.apple.com/tutorials/swiftui/interfacing-with-uikit

Unfortunately, I don't know a whole lot about SwiftUI (I'm still sticking with UIKit for a little longer. :) ), so I sadly can't commit the time to learning and then writing a sample app at the moment.

I'll leave this issue open with a "PR Requested" status attached. If anyone else would like to make a SwiftUI sample and post a PR for it, that would be super appreciated. :)

I hope that helps!

@ClaesClaes
Copy link
Author

ClaesClaes commented Jul 20, 2020 via email

CH3COOH added a commit to CH3COOH/TOCropViewController that referenced this issue Jul 25, 2020
@CH3COOH
Copy link

CH3COOH commented Jul 25, 2020

Hi, @ClaesClaes !
I've created a sample app to use the 'TOCropViewController' with the SwiftUI.

With using SwiftUI, I'm seeing an issue where 'TOCropViewController' can't be closed unless you wait for 1 second when transitioning from 'UIImagePickerController' to 'TOCropViewController'. How have you fixed this problem?

  • Xcode 11.6 / iOS 13.6

@ClaesClaes
Copy link
Author

ClaesClaes commented Jul 26, 2020 via email

@maxtomczyk
Copy link

Hi,
I "interfaced" TOCropViewController for SwiftUI. I just started my iOS development adventure, so this code probably is not perfect but it still works and can help some people to start with this repo and SwiftUI.

// FIle: ImageCropper.swift

import SwiftUI
import UIKit
import CropViewController

struct ImageCropper: UIViewControllerRepresentable{
  @Binding var image: UIImage?
  @Binding var visible: Bool
  var done: (UIImage) -> Void
  
  class Coordinator: NSObject, CropViewControllerDelegate{
    let parent: ImageCropper
    
    init(_ parent: ImageCropper){
      self.parent = parent
    }
    
    func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
      withAnimation{
        parent.visible = false
      }
      parent.done(image)
    }
    
    func cropViewController(_ cropViewController: CropViewController, didFinishCancelled cancelled: Bool) {
      withAnimation{
        parent.visible = false
      }
    }
  }
  
  func makeCoordinator() -> Coordinator {
    return Coordinator(self)
  }
  
  func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {}
  
  func makeUIViewController(context: Context) -> some UIViewController {
    let img = self.image ?? UIImage()
    let cropViewController = CropViewController(image: img)
    cropViewController.delegate = context.coordinator
    return cropViewController
  }
}

Usage:

// FIle: SomeView.swift
import SwiftUI

struct SomeView: View {
  @State private var showImageCropper = false
  @State private var tempInputImage: UIImage?

  func imageCropped(image: UIImage){
    self.tempInputImage = nil
    self.pictures[self.pickedImageIndex] = Image(uiImage: image)
  }

  var body: some View {
    if showImageCropper {
      ImageCropper(image: self.$tempInputImage, visible: self.$showImageCropper, done: self.imageCropped)
        .zIndex(10)
    }
  }
}

@yeyimilk
Copy link

yeyimilk commented Nov 9, 2020

Hi,
I "interfaced" TOCropViewController for SwiftUI. I just started my iOS development adventure, so this code probably is not perfect but it still works and can help some people to start with this repo and SwiftUI.

// FIle: ImageCropper.swift

import SwiftUI
import UIKit
import CropViewController

struct ImageCropper: UIViewControllerRepresentable{
  @Binding var image: UIImage?
  @Binding var visible: Bool
  var done: (UIImage) -> Void
  
  class Coordinator: NSObject, CropViewControllerDelegate{
    let parent: ImageCropper
    
    init(_ parent: ImageCropper){
      self.parent = parent
    }
    
    func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
      withAnimation{
        parent.visible = false
      }
      parent.done(image)
    }
    
    func cropViewController(_ cropViewController: CropViewController, didFinishCancelled cancelled: Bool) {
      withAnimation{
        parent.visible = false
      }
    }
  }
  
  func makeCoordinator() -> Coordinator {
    return Coordinator(self)
  }
  
  func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {}
  
  func makeUIViewController(context: Context) -> some UIViewController {
    let img = self.image ?? UIImage()
    let cropViewController = CropViewController(image: img)
    cropViewController.delegate = context.coordinator
    return cropViewController
  }
}

Usage:

// FIle: SomeView.swift
import SwiftUI

struct SomeView: View {
  @State private var showImageCropper = false
  @State private var tempInputImage: UIImage?

  func imageCropped(image: UIImage){
    self.tempInputImage = nil
    self.pictures[self.pickedImageIndex] = Image(uiImage: image)
  }

  var body: some View {
    if showImageCropper {
      ImageCropper(image: self.$tempInputImage, visible: self.$showImageCropper, done: self.imageCropped)
        .zIndex(10)
    }
  }
}

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

    // some how, there maybe a bug, it can not be dismissed except adding this
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
        withAnimation{
            self.parent.visible = false
        }
    }
}

@chkmls
Copy link

chkmls commented Jan 15, 2023

Sorry to reopen it , but the swiftui code return no cropper, and very tiny image. I used the exact code as this

Hi,
I "interfaced" TOCropViewController for SwiftUI. I just started my iOS development adventure, so this code probably is not perfect but it still works and can help some people to start with this repo and SwiftUI.

// FIle: ImageCropper.swift

import SwiftUI
import UIKit
import CropViewController

struct ImageCropper: UIViewControllerRepresentable{
  @Binding var image: UIImage?
  @Binding var visible: Bool
  var done: (UIImage) -> Void
  
  class Coordinator: NSObject, CropViewControllerDelegate{
    let parent: ImageCropper
    
    init(_ parent: ImageCropper){
      self.parent = parent
    }
    
    func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
      withAnimation{
        parent.visible = false
      }
      parent.done(image)
    }
    
    func cropViewController(_ cropViewController: CropViewController, didFinishCancelled cancelled: Bool) {
      withAnimation{
        parent.visible = false
      }
    }
  }
  
  func makeCoordinator() -> Coordinator {
    return Coordinator(self)
  }
  
  func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {}
  
  func makeUIViewController(context: Context) -> some UIViewController {
    let img = self.image ?? UIImage()
    let cropViewController = CropViewController(image: img)
    cropViewController.delegate = context.coordinator
    return cropViewController
  }
}

Usage:

// FIle: SomeView.swift
import SwiftUI

struct SomeView: View {
  @State private var showImageCropper = false
  @State private var tempInputImage: UIImage?

  func imageCropped(image: UIImage){
    self.tempInputImage = nil
    self.pictures[self.pickedImageIndex] = Image(uiImage: image)
  }

  var body: some View {
    if showImageCropper {
      ImageCropper(image: self.$tempInputImage, visible: self.$showImageCropper, done: self.imageCropped)
        .zIndex(10)
    }
  }
}

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

    // some how, there maybe a bug, it can not be dismissed except adding this
    DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
        withAnimation{
            self.parent.visible = false
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants