如何裁剪 4 * 3 的图像,在 photoLibrary 或在照相机swift

标签: ios Swift
发布时间: 2017/3/6 23:05:27
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

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

现在,我只能裁剪方形图像,我也不知道如何裁剪 4 * 3 的图像。

这里是代码的我的一部分

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

enter image description here

解决方法 1:

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

目的︰ 作物图像与预定义的比例。 方法︰

  • 在 viewcontroller-我们需要添加滚动视图-所以,我们可以把图像放在那里和缩放或平移如果需要。
  • 设置约束的滚动高度和视图高度比 (可能是 0.8)-留下 20%的要将按钮的视图。
  • 设置约束的滚动高度和宽度比例 (在本例中 4:3) 以编程方式添加 Imageview (集大小的所选的图像的高度和宽度)
  • 设置滚动视图最小和最大的缩放比例,以适应较小和较大的图像在滚动视图正确。
  • 在显示的图像的同时,我们将确保 imageview 也适合的高度或宽度。

如果我们可以在 4:3 比例滚动视图中查看只有图像,现在运行应用程序请参阅。

为保存选项只是地图的滚动视图坐标和从 imageview 所得到的图像 (在这里,我们需要使用的缩放比例)

代码︰

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)")
        }
赞助商