我正在尝试使用UIScrollview
实现图像缩放功能。在哪里我保持图像适合方面。
图片位于UIScrollView
内,图片框的类似于UIScrollView
这是我的代码。
class CroppingImageViewController: UIViewController, UIScrollViewDelegate{
var image:UIImage = UIImage()
@IBOutlet var imageview:UIImageView!
@IBOutlet var scrollview:UIScrollView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
imageview.image = image
scrollview.contentSize = imageview.frame.size;
scrollview.minimumZoomScale = 1.0;
scrollview.maximumZoomScale = 4.0;
scrollview.setZoomScale(scrollview.minimumZoomScale, animated: true)
scrollview.delegate = self;
}
func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
return imageview
}
}
What's actually happening
What required
我的要求是图像应该滚动到它的内容。
答案 0 :(得分:0)
self.automaticallyAdjustsScrollViewInsets=NO;
根据Apple文档,默认情况下,autoAdjustScrollViewInsets为YES。
所以它试图控制你的滚动视图。
在viewdidload方法中添加它,然后尝试。
答案 1 :(得分:0)
我正在寻找相同的东西一段时间。而且我确实喜欢这个。它对我有用。
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
imageView.image = image
imageView.contentMode = UIViewContentMode.center
imageView.frame = CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height)
ScrollView.contentSize = imageView.frame.size
let scrollViewFrame = ScrollView.frame
let scaleWidth = scrollViewFrame.size.width / ScrollView.contentSize.width
let scaleHeight = scrollViewFrame.size.height/ScrollView.contentSize.height
let minScale = min(scaleHeight, scaleWidth)
ScrollView.minimumZoomScale = minScale
ScrollView.maximumZoomScale = 1
ScrollView.zoomScale = minScale
ScrollView.clipsToBounds = true
centerScrollViewContents()
picker.dismiss(animated: true, completion: nil)
}
func centerScrollViewContents(){
let boundsSize = ScrollView.bounds.size
var contentsFrame = imageView.frame
if contentsFrame.size.width < boundsSize.width{
contentsFrame.origin.x = (boundsSize.width - contentsFrame.size.width)/2
}else{
contentsFrame.origin.x = 0
}
if contentsFrame.size.height < boundsSize.height{
contentsFrame.origin.y = (boundsSize.height - contentsFrame.size.height)/2
}else{
contentsFrame.origin.y = 0
}
imageView.frame = contentsFrame
}