本文介绍了如何在 Swift 中转换 Objective-C TOCropViewController 委托方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我想转换 TOCropViewController 委托方法.我在 swift 项目中使用了 bridge_header &在bridge_header 文件中调用TOCropViewController 类.然后我将委托方法放在viewcontroller.swift 中.到目前为止,我这样做了

Actually I want to convert TOCropViewController delegate method.I used bridge_header in swift project & call TOCropViewController class in bridge_header file.Then I put delegate method in viewcontroller.swift.As far I did this

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

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

But the how can convert below objective c TOCropViewController delegate method in swift.

#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...

I search lot on google,but can not found this..Some link I search https://github.com/TimOliver/TOCropViewController/issues/25 & https://github.com/TimOliver/TOCropViewController/issues/71...

推荐答案

 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  }
    }

这篇关于如何在 Swift 中转换 Objective-C TOCropViewController 委托方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 07:43