我尝试了许多不同的方法来实现扩展图像和捏缩放。我找到了关于如何使用我的项目中保存的图像执行此操作的教程,但是我正在从Firebase中检索我的图像。我希望一个图像占据ProfileViewController屏幕的一半,其他图片可以扩展整页,并且还具有缩放功能。我现在拥有的代码可以做到这一点,但缩放和位置不干净或不易使用。
var newImageView: UIImageView!
@IBAction func imageTapped(_ sender: UITapGestureRecognizer) {
let imageView = sender.view as! UIImageView
let scrollView = UIScrollView(frame: self.view.frame)
newImageView = UIImageView(image: imageView.image)
newImageView.frame = self.view.frame
newImageView.backgroundColor = .black
newImageView.contentMode = .top
newImageView.isUserInteractionEnabled = true
let tap = UITapGestureRecognizer(target: self, action: #selector(dismissFullscreenImage))
scrollView.addGestureRecognizer(tap)
scrollView.delegate = self
scrollView.minimumZoomScale = 1.0
scrollView.maximumZoomScale = 3.0
scrollView.addSubview(newImageView)
self.view.addSubview(scrollView)
}
func viewForZooming(in scrollView: UIScrollView) -> UIView?
{
return newImageView;
}
func dismissFullscreenImage(_ sender: UITapGestureRecognizer) {
sender.view?.removeFromSuperview()
}
答案 0 :(得分:0)
以下是我以编程方式创建scrollview和imageview,此处 img 是PHAsset
,我正在从图库中选择图片。
在这里我正在做的事情,选择一个图像并移动到其他视图控制器,我根据我的imageview调整该图像的大小
因此,缩放后图像不会模糊。
最小缩放是图像视图边界,最大缩放是4.0。
func singleVC1() {
let v = UIView(frame: CGRect(x: 40, y: 50, width: self.view.frame.size.width - 80, height: view.frame.size.height - 200))
let view1 = UIView(frame: CGRect(x: 0, y: 0, width: v.frame.size.width, height: v.frame.size.height/2 - 1))
let scroll1 = UIScrollView.init(frame: view1.bounds)
let imgview = UIImageView.init(frame: (CGRect)(origin: CGPoint.zero, size: self.returnAspectSize(targetWidth: view.frame.size.width, img: img)))
self.setZoomScale(scrollview: scroll1, imageview: imgview)
imgview.image = self.getUIImage(asset: img)
scroll1.maximumZoomScale = 4.0;
scroll1.delegate = self;
scroll1.bounces = false
scroll1.contentSize = imgview.frame.size
scroll1.addSubview(imgview)
view1.layer.borderColor = UIColor.red.cgColor
view1.layer.borderWidth = 5.0
view1.addSubview(scroll1)
v.addSubview(view1)
let view2 = UIView(frame: CGRect(x: 0, y: v.frame.size.height/2+1, width: v.frame.size.width, height: v.frame.size.height/2 - 1))
let scroll2 = UIScrollView.init(frame: view2.bounds)
let imgview1 = UIImageView.init(frame: (CGRect)(origin: CGPoint.zero, size: self.returnAspectSize(targetWidth: view.frame.size.width, img: img)))
imgview1.image = self.getUIImage(asset: img)
self.setZoomScale(scrollview: scroll2, imageview: imgview1)
scroll2.bounces = false
scroll2.maximumZoomScale = 4.0;
scroll2.delegate = self;
view2.layer.borderColor = UIColor.red.cgColor
view2.layer.borderWidth = 5.0
scroll2.contentSize = imgview1.frame.size
scroll2.addSubview(imgview1)
view2.addSubview(scroll2)
v.addSubview(view2)
self.view.addSubview(v)
}
func setZoomScale(scrollview : UIScrollView, imageview : UIImageView) {
var minZoom = min(self.view.bounds.size.width / imageview.bounds.size.width, self.view.bounds.size.height / imageview.bounds.size.height);
if (minZoom > 1.0) {
minZoom = 1.0;
}
scrollview.minimumZoomScale = minZoom;
scrollview.zoomScale = minZoom;
}
func returnAspectSize(targetWidth : CGFloat, img : PHAsset) -> CGSize {
let img1 = self.getUIImage(asset: self.img)
let targetwidth = targetWidth
let scalefactor = (CGFloat)(targetwidth) / (CGFloat)((img1?.size.width)!)
let targetheight = (img1?.size.height)! * scalefactor
let targesize = CGSize(width: (CGFloat)(targetwidth), height: targetheight)
return targesize
}
试试这个。我相信这会对你有帮助。