如何快速在PhotoLibrary或相机中裁剪4 * 3图像 [英] How to crop a 4*3 image in photoLibrary or camera in swift

查看:162
本文介绍了如何快速在PhotoLibrary或相机中裁剪4 * 3图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用相机或从photoLibrary中选择后裁剪图像。

I want to crop the image after using camera or select from photoLibrary.

目前,我只能裁剪方形图像,不知道如何裁剪一张4 * 3的图片。

For now, I can only crop the square image and I have no idea how to crop a 4*3 image.

这是我的一部分代码

let imagePicker : UIImagePickerController = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .PhotoLibrary
imagePicker.allowsEditing = true

推荐答案

我不知道不允许链接作为答案。在这里,我将尝试回答这个问题。

I did not know that links are not permitted as answer. Here I will try to answer the question.

目标:以预定比例裁剪图像。
方法:

Aim: Crop image with a predefined ratio. Approach:


  • 在视图控制器中-我们需要添加滚动视图-这样我们才能
    放置图像

  • 设置滚动视图高度和视图高度比例的约束(可能
    为0.8)-保留视图的20%来放置按钮

  • 设置滚动视图的高度和宽度比例的约束(在此
    情况下为4:3)以编程方式添加Imageview(设置所选
    图像的高度和宽度)

  • 设置滚动视图的最小和最大缩放比例,以在滚动视图中正确放置较小和
    的图像。

  • 在显示图像时我们将确保imageview适合
    的高度或宽度。

  • In the viewcontroller - we need to add a scrollview - so that we can put the image there and zoom or pan if required.
  • Set a constrain for the scrollview height and view height ratio(may be 0.8) - left 20% of the view to put the buttons.
  • set a constrain for the scrollview height and width ratio (in this case 4:3) Programmatically add Imageview (set size of the selected image's height and width )
  • Set scrollview minimum and maximum zoom scale to fit smaller and larger images in the scrollview properly.
  • while displaying the image we will make sure imageview either fits by height or width.

现在运行应用程序,看看是否只能查看以4:3比例的滚动视图显示图像。

Now run the application see if we can view only the image in an 4:3 ratio scrollview.

对于save选项,只需映射滚动视图的坐标并从imageview获取图像(此处需要使用缩放规模)

For the save option just map the coordinates of the scrollview and get the image from the imageview (here we need to use the zoom scale)

代码:

var imgview: UIImageView!
    var imagepicked:UIImage!
    var  minZoomScale:CGFloat!
    let picker = UIImagePickerController()
    @IBOutlet weak var scrollViewSquare: UIScrollView!

    override func viewDidLoad() {
        super.viewDidLoad()
        picker.delegate = self
        scrollViewSquare.delegate = self
    }
func imagePickerController(
        picker: UIImagePickerController,
        didFinishPickingMediaWithInfo info: [String : AnyObject])
    {
        imagepicked = (info[UIImagePickerControllerOriginalImage] as? UIImage)!
        ImageViewInit()
        dismissViewControllerAnimated(false, completion: nil)        
    }

    @IBAction func Pick(sender: AnyObject) {
        picker.allowsEditing = false
        picker.sourceType = .PhotoLibrary
        presentViewController(picker, animated: true, completion: nil)
    }
func ImageViewInit(){
       imgview = UIImageView()
        imgview.frame =  CGRectMake(0, 0, imagepicked.size.width, imagepicked.size.height)
        imgview.image = imagepicked
        imgview.contentMode = .ScaleAspectFit
        imgview.backgroundColor = UIColor.lightGrayColor()
        scrollViewSquare.maximumZoomScale=4;
        scrollViewSquare.minimumZoomScale=0.02;
        scrollViewSquare.bounces=true;
        scrollViewSquare.bouncesZoom=true;
        scrollViewSquare.contentMode = .ScaleAspectFit
        scrollViewSquare.contentSize = imagepicked.size
        scrollViewSquare.autoresizingMask = UIViewAutoresizing.FlexibleWidth
        scrollViewSquare.addSubview(imgview)
        setZoomScale()
    }
    //fit imageview in the scrollview
    func setZoomScale(){
        let imageViewSize = imgview.bounds.size
        let scrollViewSize = scrollViewSquare.bounds.size
        let widthScale = scrollViewSize.width / imageViewSize.width
        let heightScale = scrollViewSize.height / imageViewSize.height
        minZoomScale = max(widthScale, heightScale)
        scrollViewSquare.minimumZoomScale = minZoomScale
        scrollViewSquare.zoomScale = minZoomScale
    }

    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imgview
    }
//mapping the image from the scrollview to imageview to get the desired ratio 
    @IBAction func Save(sender: AnyObject) {
            let offset = scrollViewSquare.contentOffset
            let visibleRect: CGRect = CGRectMake(offset.x, offset.y, offset.x+scrollViewSquare.frame.width, offset.y+scrollViewSquare.frame.height)
            let visibleImgRect: CGRect = CGRectMake(offset.x/scrollViewSquare.zoomScale, offset.y/scrollViewSquare.zoomScale, (offset.x+scrollViewSquare.frame.width)/scrollViewSquare.zoomScale, (offset.y+scrollViewSquare.frame.height)/scrollViewSquare.zoomScale)
            print("content offset now\(offset) and visible rect is \(visibleRect)  - zoomlevel \(scrollViewSquare.zoomScale)  now image \(imagepicked.size) and current visible area in image is \(visibleImgRect)")
        }

这篇关于如何快速在PhotoLibrary或相机中裁剪4 * 3图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆