如何在 Swift 中转换 Objective-C TOCropViewController 委托方法?

Posted

技术标签:

【中文标题】如何在 Swift 中转换 Objective-C TOCropViewController 委托方法?【英文标题】:How to convert Objective-C TOCropViewController delegate method in Swift? 【发布时间】:2016-04-28 04:18:32 【问题描述】:

其实我想转换TOCropViewController委托方法。我在swift项目中使用bridge_header并在bridge_header文件中调用TOCropViewController类。然后我把委托方法放在viewcontroller.swift中。到目前为止我做了这个

let cropViewController = TOCropViewController(image:image)
cropViewController.delegate = self
self.presentViewController(cropViewController, animated: true,     completion: nil)
self.imageView.image = image

但是如何在 swift 中转换目标 c TOCropViewController 委托方法。

#pragma mark - Cropper Delegate -


- (void)cropViewController:(TOCropViewController *)cropViewController    didCropToImage:(UIImage *)image withRect:(CGRect)cropRect angle: (NSInteger)angle

self.imageView.image = image;
[self layoutImageView];

self.navigationItem.rightBarButtonItem.enabled = YES;

CGRect viewFrame = [self.view convertRect:self.imageView.frame     toView:self.navigationController.view];
         self.imageView.hidden = YES;
      [cropViewController    dismissAnimatedFromParentViewController:self withCroppedImage:image   toFrame:viewFrame completion:^
    self.imageView.hidden = NO;
];
  

 #pragma mark - Image Picker Delegate -
 - (void)imagePickerController:(UIImagePickerController *)picker  didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo

[self dismissViewControllerAnimated:YES completion:^
    self.image = image;
    TOCropViewController *cropController = [[TOCropViewController     alloc] initWithImage:image];
         cropController.delegate = self;

         // Uncomment this to test out locked aspect ratio sizes
        // cropController.defaultAspectRatio =       TOCropViewControllerAspectRatiosquare;
       // cropController.aspectRatioLocked = YES;

       // Uncomment this to place the toolbar at the top of the view controller
       // cropController.toolbarPosition =    TOCropViewControllerToolbarPositionTop;

         [self presentViewController:cropController animated:YES completion:nil];
  ];
    

      - (void)imagePickerControllerDidCancel:(UIImagePickerController     *)picker
    
    [self dismissViewControllerAnimated:YES completion:nil];
     

   #pragma mark - Gesture Recognizer -
  - (void)didTapImageView
  
    TOCropViewController *cropController = [[TOCropViewController alloc]   initWithImage:self.image];
    cropController.delegate = self;

   // Uncomment this to place the toolbar at the top of the view con  troller
      // cropController.toolbarPosition =       TOCropViewControllerToolbarPositionTop;

        [self presentViewController:cropController animated:YES completion:nil];
       

我在谷歌上搜索了很多,但找不到这个..我搜索的一些链接https://github.com/TimOliver/TOCropViewController/issues/25 &https://github.com/TimOliver/TOCropViewController/issues/71...

【问题讨论】:

【参考方案1】:
 func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!)
    
        self.dismissViewControllerAnimated(true, completion:  () -> Void in
            if image != nil
            
                let cropController:TOCropViewController = TOCropViewController(image: image)
                cropController.delegate=self
                self.presentViewController(cropController, animated: true, completion: nil)
            
        )
    

    func imagePickerControllerDidCancel(picker: UIImagePickerController)
    
        picker.dismissViewControllerAnimated(true, completion:  () -> Void in )
    

    // -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
    //        Cropper Delegate
    // -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-

    func cropViewController(cropViewController: TOCropViewController!, didCropToImage image: UIImage!, withRect cropRect: CGRect, angle: Int)
    
        cropViewController.dismissViewControllerAnimated(true)  () -> Void in
            self.imageView.image = image
        
    

    func cropViewController(cropViewController: TOCropViewController!, didFinishCancelled cancelled: Bool)
    
        cropViewController.dismissViewControllerAnimated(true)  () -> Void in  
    

【讨论】:

谢谢 Jayesh....我会使用你的 swift 方法.....并在 swift 中完全转换我的代码。 我在尝试分配委托时出错,只是为了评论仍然需要在类声明中添加 TOCropViewControllerDelegate!

以上是关于如何在 Swift 中转换 Objective-C TOCropViewController 委托方法?的主要内容,如果未能解决你的问题,请参考以下文章

如何将 Objective-C 的完成块转换为 Swift?

如何将 .hour() 和 .minute() 从 Swift 转换为 Objective-C?

Objective-C 方法签名在 Swift 转换后是一样的

将 Objective-C 中的函数转换为 swift

再次将 textKit 从 Objective-C 转换为 Swift

将UILabel子类从Objective-C转换为Swift