[英]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:
做法:
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.