在 UIScrollview 上缩放图像后,它会滚动到图像区域之外
Posted
技术标签:
【中文标题】在 UIScrollview 上缩放图像后,它会滚动到图像区域之外【英文标题】:After zooming image on UIScrollview, it scroll outside of image areas 【发布时间】:2016-03-04 13:11:59 【问题描述】:我正在尝试使用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 happeningWhat required 我的要求是图像应该向上滚动到它的内容。
【问题讨论】:
【参考方案1】:self.automaticallyAdjustsScrollViewInsets=NO;
根据苹果文档,automaticAdjustScrollViewInsets 默认为 YES。
所以它试图控制你的滚动视图。
将此添加到您的 viewdidload 方法中,然后尝试。
【讨论】:
还是一样。不走运!!【参考方案2】:我一直在寻找同样的东西。我确实喜欢这个。它对我有用。
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
【讨论】:
以上是关于在 UIScrollview 上缩放图像后,它会滚动到图像区域之外的主要内容,如果未能解决你的问题,请参考以下文章