Tocropviewcontroller: 使用 TOCropViewController 的 SwiftUI 示例

创建于 2020-07-14  ·  6评论  ·  资料来源: TimOliver/TOCropViewController

嗨,有使用 TOCropViewController 的 SwiftUI 示例吗? 我刚刚开始学习 SwiftUI 并且以前没有 UIViewcontroller 的经验,所以会帮助我前进。

pr requested question

最有用的评论

你好,
我为 SwiftUI“连接”了 TOCropViewController。 我刚刚开始我的 iOS 开发冒险,所以这段代码可能并不完美,但它仍然有效,可以帮助一些人开始使用这个 repo 和 SwiftUI。

// FIle: ImageCropper.swift

import SwiftUI
import UIKit
import CropViewController

struct ImageCropper: UIViewControllerRepresentable{
  <strong i="7">@Binding</strong> var image: UIImage?
  <strong i="8">@Binding</strong> 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
  }
}

用法:

// FIle: SomeView.swift
import SwiftUI

struct SomeView: View {
  <strong i="12">@State</strong> private var showImageCropper = false
  <strong i="13">@State</strong> 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)
    }
  }
}

所有6条评论

嗨@ClaesClaes! 哇哦! 欢迎来到 Swift 社区!

之前有几个人问过这个问题。 Apple 发布了一些教程,希望能指出正确的方向: https :

不幸的是,我对 SwiftUI 知之甚少(我仍然坚持使用 UIKit 一段时间。:)),所以遗憾的是,我目前无法投入时间学习然后编写示例应用程序。

我将保留此问题并附加“PR 请求”状态。 如果其他人想制作 SwiftUI 示例并为其发布 PR,那将不胜感激。 :)

我希望这有帮助!

嗨蒂姆,
感谢你的回复。 我已经设法将 TOCropViewController 集成到我的 SwiftUI 项目中,因此它(大部分)现在看起来不错。 如果时间允许,我可以发布一些代码+说明。
干杯,
克拉斯

2020 年 7 月 20 日下午 2:37,Tim Oliver [email protected]写道:

@ClaesClaes https://github.com/ClaesClaes ! 哇哦! 欢迎来到 Swift 社区!

之前有几个人问过这个问题。 Apple 发布了一些教程,希望能指明正确的方向: https : https://developer.apple.com/tutorials/swiftui/interface-with-工具包
不幸的是,我对 SwiftUI 知之甚少(我仍然坚持使用 UIKit 一段时间。:)),所以遗憾的是,我目前无法投入时间学习然后编写示例应用程序。

我将保留此问题并附加“PR 请求”状态。 如果其他人想制作 SwiftUI 示例并为其发布 PR,那将不胜感激。 :)


你收到这个是因为你被提到了。
直接回复本邮件,在 GitHub 上查看https://github.com/TimOliver/TOCropViewController/issues/421#issuecomment-660858630 ,或者退订https://github.com/notifications/unsubscribe-auth/ACOX6SWCSSAJWXCJ7KUWH2LR4PX2NANCNFSMQ4OZHUW

嗨, @ClaesClaes
我创建了一个示例应用程序,以将“TOCropViewController”与 SwiftUI 结合使用。

使用 SwiftUI 时,我看到一个问题,除非在从“UIImagePickerController”转换到“TOCropViewController”时等待 1 秒,否则无法关闭“TOCropViewController”。 你是如何解决这个问题的?

  • Xcode 11.6 / iOS 13.6

嗨健二,
我在我的实施过程中没有看到这种延迟。 本周我无法对此进行测试,但可以稍后查看。

克拉斯

从我的手机发送。 为简洁和拼写错误道歉

在2020年7月25日,在22:12,和田贤治[email protected]写道:


嗨, @ClaesClaes
我创建了一个示例应用程序,以将“TOCropViewController”与 SwiftUI 结合使用。

https://github.com/CH3COOH/TOCropViewController/tree/issue/421
使用 SwiftUI 时,我看到一个问题,除非在从“UIImagePickerController”转换到“TOCropViewController”时等待 1 秒,否则无法关闭“TOCropViewController”。 你是如何解决这个问题的?

Xcode 11.6 / iOS 13.6

你收到这个是因为你被提到了。
直接回复此邮件,在 GitHub 上查看,或取消订阅。

你好,
我为 SwiftUI“连接”了 TOCropViewController。 我刚刚开始我的 iOS 开发冒险,所以这段代码可能并不完美,但它仍然有效,可以帮助一些人开始使用这个 repo 和 SwiftUI。

// FIle: ImageCropper.swift

import SwiftUI
import UIKit
import CropViewController

struct ImageCropper: UIViewControllerRepresentable{
  <strong i="7">@Binding</strong> var image: UIImage?
  <strong i="8">@Binding</strong> 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
  }
}

用法:

// FIle: SomeView.swift
import SwiftUI

struct SomeView: View {
  <strong i="12">@State</strong> private var showImageCropper = false
  <strong i="13">@State</strong> 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)
    }
  }
}

你好,
我为 SwiftUI“连接”了 TOCropViewController。 我刚刚开始我的 iOS 开发冒险,所以这段代码可能并不完美,但它仍然有效,可以帮助一些人开始使用这个 repo 和 SwiftUI。

// FIle: ImageCropper.swift

import SwiftUI
import UIKit
import CropViewController

struct ImageCropper: UIViewControllerRepresentable{
  <strong i="8">@Binding</strong> var image: UIImage?
  <strong i="9">@Binding</strong> 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
  }
}

用法:

// FIle: SomeView.swift
import SwiftUI

struct SomeView: View {
  <strong i="13">@State</strong> private var showImageCropper = false
  <strong i="14">@State</strong> 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)
    }
  }
}

funccropViewController(_cropViewController:CropViewController,didCropToImage图像:UIImage,withRectcropRect:CGRect,角度:Int){
parent.done(图像)

    // 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
        }
    }
}
此页面是否有帮助?
0 / 5 - 0 等级