在一个视图中,我选择了一些图像,然后单击“完成”按钮,将这些图像传递到另一个视图(滚动视图),并导航到该视图。
在该滚动视图的viewDidLoad
中,我将图像添加到滚动视图中,就像这样...
for i in 0..<images.count {
let imageView = UIImageView()
let x = self.view.frame.size.width * CGFloat(i)
imageView.frame = CGRect(x: x, y: 0, width: self.view.frame.width, height: self.view.frame.height)
imageView.contentMode = .scaleAspectFit
imageView.image = images[i]
scrollView.contentSize.width = scrollView.frame.size.width * CGFloat(i + 1)
scrollView.addSubview(imageView)
let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItem.Style.plain, target: self, action: #selector(self.goBack))
navigationItem.leftBarButtonItem = backButton
let cropButton = UIBarButtonItem(title: "Crop", style: UIBarButtonItem.Style.plain, target: self, action: #selector(self.crop))
navigationItem.rightBarButtonItem = cropButton
}
现在,在此滚动视图的顶部,有一个名为“ CROP”的按钮,单击该按钮,我导航到某个视图,该视图是用于裁剪的界面(该界面是用于裁剪图像的库的一部分)。现在,在裁剪图像之后,我在该界面中单击“完成”按钮,这将导致调用此函数(该库本身的一部分)...
此功能为我提供了裁剪后的图像。
func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
print(image) --> This gives the cropped image
}
单击裁剪界面上的完成按钮时,我再次返回到滚动视图。为了获得更新的图像,即滚动视图中的裁剪图像,我尝试了类似的方法。
func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
croppedImage = image
self.images[Int(currentPage)] = image // currentPage is the current index of scrollview
//Below lines of code are the same lines written in viewDidLoad
for i in 0..<images.count {
let imageView = UIImageView()
let x = self.view.frame.size.width * CGFloat(i)
imageView.frame = CGRect(x: x, y: 0, width: self.view.frame.width, height: self.view.frame.height)
imageView.contentMode = .scaleAspectFit
imageView.image = self.images[i]
scrollView.contentSize.width = scrollView.frame.size.width * CGFloat(i + 1)
scrollView.addSubview(imageView)
let backButton = UIBarButtonItem(title: "Back", style: UIBarButtonItem.Style.plain, target: self, action: #selector(self.goBack))
navigationItem.leftBarButtonItem = backButton
let cropButton = UIBarButtonItem(title: "Crop", style: UIBarButtonItem.Style.plain, target: self, action: #selector(self.crop))
navigationItem.rightBarButtonItem = cropButton
}
}
现在,这确实为我提供了滚动视图中的更新图像。但是问题是滚动视图从第一张图像开始(即单击裁剪界面上的``完成''按钮时,我会进入滚动视图中图像列表中的第一张图像)。但是我想查看裁剪/更新的图像。
要实现此目标,我可以做些什么改变?
答案 0 :(得分:0)
一旦您用编辑过的图片更新了滚动视图,请在currentPage
方法末尾相对于cropViewController:
更改滚动视图的内容偏移量,
let xPosition = scrollView.frame.size.width * CGFloat(currentPage) //or currentPage + 1 depends on your currentPage logic
scrollView.setContentOffset(CGPoint(x: xPosition, y: 0), animated: true)