gpt4 book ai didi

ios - UIScrollView 中的 UIImageView 缩放和捏合

转载 作者:行者123 更新时间:2023-11-30 12:30:28 25 4
gpt4 key购买 nike

我尝试了许多不同的方法来实现扩展图像和捏合缩放。我找到了有关如何使用项目中保存的图像执行此操作的教程,但是我正在从 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()
}

profilePageView

enter image description here

最佳答案

这里我正在以编程方式创建 ScrollView 和 ImageView ,这里img是一个PHAsset,我正在从图库中选择图像。

这是我正在做的事情,选择一个图像并移动到其他 View Controller ,然后根据我的 ImageView 调整该图像的大小所以缩放后图像不会模糊。

最小缩放受 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
}

试试这个。我相信这会对您有所帮助。

关于ios - UIScrollView 中的 UIImageView 缩放和捏合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43624834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com