回答:在编辑中删除图像以供将来参考,保存图像由Matt友好回答。
我有一系列图像对象(Firebase上的url和ID)。它们被添加到滚动视图中。我用这段代码添加了长按手势:
func addToScroll(array:[ImageModel]) {
for i in 0..<array.count {
let currentImage = array[i]
let Resource = URL(string: currentImage.url)
let scrollWidth = self.imageScrollView.frame.size.width
let scrollheight = self.imageScrollView.frame.size.height
let newX = scrollWidth * CGFloat(i)
let imageview = SLImageView(frame: CGRect(x:((scrollWidth / 2) - 75) + newX , y:((scrollheight / 2) - 90) ,width:150, height:150))
imageview.kf.setImage(with: Resource)
imageview.contentMode = .scaleAspectFill
imageview.clipsToBounds = true
imageview.isUserInteractionEnabled = true
let longPress = UILongPressGestureRecognizer(target: self, action: #selector(deleteImageAlert(_:)))
imageview.addGestureRecognizer(longPress)
self.imageScrollView.addSubview(imageview)
self.imageScrollView.clipsToBounds = false
self.imageScrollView.contentSize = CGSize(width:scrollWidth + newX, height:scrollheight)
}
}
@IBAction func deleteImageAlert(_ sender: UILongPressGestureRecognizer) {
let alert = UIAlertController(title: "Save or Delete?", message: "Do you want to save or delete this image?", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "Save", style: UIAlertActionStyle.default, handler: { action in
let imageview = sender.view as! SLImageView
self.saveTriggered(image: imageview.image!)
}))
alert.addAction(UIAlertAction(title: "Delete", style: UIAlertActionStyle.destructive, handler: { action in
let imageview = sender.view as! SLImageView
let id2 = imageview.imageID
self.deleteTriggered(id:id2!)
}))
alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
func deleteTriggered(id:String) {
Database.database().reference().child("MeetingPhotos").child(currentMeeting.meetingID).child(id).removeValue { (error, ref) in
if error != nil {
print(error!)
}
}
Storage.storage().reference().child("meetingPhotos").child(currentMeeting.meetingID).child(id).delete { (err) in
if err != nil {
print(err!)
}
}
}
func saveTriggered(image:UIImage) {
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
我现在正在进行删除和保存,但是如何将图像对象传输到删除触发器,以便我可以使用ID和图像本身在Firebase上删除它以保存简单UIImageWriteToSavedPhotosAlbum的触发器?
编辑:我在Image子类本身为图像ID创建了一个变量(在我的情况下是SLImageView),用于删除存储和数据库referance中的图像,修复了上面的代码。然后,Firebase观察者会清除并再次添加子视图
这只是提前规划的问题。这是你的代码;写它,以便它做你想要它做的!考虑一下这段代码(与你的代码相同,但我已经清理了一点,我已经减少了它,所以我们只关注saveTriggered
部分用于教学目的):
@IBAction func deleteImageAlert(_ sender: UILongPressGestureRecognizer) {
let alert = UIAlertController(title: "Save or Delete?", message: "Do you want to save or delete this image?", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Save", style: UIAlertActionStyle.default, handler: { _ in
self.saveTriggered()
}))
self.present(alert, animated: true)
}
func saveTriggered() {
print("save")
}
现在做一些思考。问问自己:sender
一开始是什么?这是长按手势识别器。好吧,那么它的view
就是添加它的图像视图。所以现在我们知道了图像视图,所以我们知道image
。这就是我们想要的。
所以现在修复你的saveTriggered
,以便你可以将图像传递给它:
func saveTriggered(image:UIImage) {
// do something with image
}
最后,修复您的通话,以便将该图像传递到其中:
alert.addAction(UIAlertAction(title: "Save", style: UIAlertActionStyle.default, handler: { _ in
let imageview = sender.view as! UIImageView
self.saveTriggered(image:imageview.image!)
}))