简体   繁体   English

如何快速在PhotoLibrary或相机中裁剪4 * 3图像

[英]How to crop a 4*3 image in photoLibrary or camera in swift

I want to crop the image after using camera or select from photoLibrary. 我想在使用相机后裁切图像或从photoLibrary中选择图像。

For now, I can only crop the square image and I have no idea how to crop a 4*3 image. 目前,我只能裁剪正方形图像,不知道如何裁剪4 * 3图像。

Here is my part of code 这是我的部分代码

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: 做法:

  • In the viewcontroller - we need to add a scrollview - so that we can put the image there and zoom or pan if required. 在viewcontroller中-我们需要添加一个scrollview-以便我们可以将图像放在此处并根据需要缩放或平移。
  • Set a constrain for the scrollview height and view height ratio(may be 0.8) - left 20% of the view to put the buttons. 为scrollview的高度和视图的高度比例设置一个约束(可能为0.8)-在视图的左20%处放置按钮。
  • 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 ) 设置滚动视图的高度和宽度比例的约束(在这种情况下为4:3)以编程方式添加Imageview(设置所选图像的高度和宽度的大小)
  • Set scrollview minimum and maximum zoom scale to fit smaller and larger images in the scrollview properly. 设置scrollview的最小和最大缩放比例,以正确地在scrollview中容纳越来越大的图像。
  • while displaying the image we will make sure imageview either fits by height or width. 在显示图像时,我们将确保imageview可以适合高度或宽度。

Now run the application see if we can view only the image in an 4:3 ratio scrollview. 现在运行该应用程序,看看是否只能以4:3的比例滚动查看图像。

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) 对于保存选项,只需映射滚动视图的坐标并从图像视图获取图像(这里我们需要使用缩放比例)

Code: 码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM