我想在使用相机后裁剪图像或从照片库中选择图像。
目前,我只能裁剪方形图像,不知道如何裁剪 4*3 图像。
这是我的部分代码
let imagePicker : UIImagePickerController = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .PhotoLibrary
imagePicker.allowsEditing = true
我不知道不允许链接作为答案。在这里我尝试回答一下这个问题。
目标:以预定义的比例裁剪图像。
方法:
- 在视图控制器中 - 我们需要添加一个滚动视图 - 这样我们就可以
将图像放在那里并根据需要进行缩放或平移。
- 设置滚动视图高度和视图高度比例的约束(可以
为 0.8) - 留出 20% 的视图来放置按钮。
- 设置滚动视图高度和宽度比例的约束(在此
情况 4:3) 以编程方式添加 Imageview(设置所选内容的大小)
图像的高度和宽度)
- 设置滚动视图最小和最大缩放比例以适应较小和
滚动视图中的较大图像正确。
- 在显示图像时,我们将确保 imageview 适合
高度或宽度。
现在运行应用程序,看看我们是否只能以 4:3 比例的滚动视图查看图像。
对于保存选项,只需映射滚动视图的坐标并从图像视图获取图像(这里我们需要使用缩放比例)
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)")
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)