提问者:小点点

如何在Swift中使UImageView照片在一段时间间隔后消失


我正在开发一个用于分割的相机应用程序,我设法捕获了现场照片,以缩略图的形式显示在UIImageView中。 现在我想让它在3秒内消失,如果用户没有触摸(像IOS相机在拍照时的行为)。 你能帮我如何实现吗? 请看下面我的代码的相关部分。 事先多谢。

// MARK: -- Action to Capture Photo

extension ViewController {
    
    @IBAction func photoButtonAction(sender: UIButton) {
        CameraManager.shared.capture { [weak self] (pixelBuffer, sampleBuffer) in
            self?.handleCameraOutput(pixelBuffer: pixelBuffer, sampleBuffer: sampleBuffer, onFinish: { (image) in
                self?.imagePreview.image = image
                
                let pngData = image!.pngData()
                let compressedData = UIImage(data: pngData!)
                self!.writeToPhotoAlbum(image: compressedData!)

            })
        }
                
    }
        
        func writeToPhotoAlbum(image: UIImage) {
            UIImageWriteToSavedPhotosAlbum(image, self, #selector(saveError), nil)
    
        }

        @objc func saveError(_ image: UIImage, didFinishSavingWithError error: Error?, contextInfo: UnsafeRawPointer) {
            
        }
        
    }

共1个答案

匿名用户

您可以使用DispatchQueue.main.AsyncAfter(deadline:)方法,如下所示:

var noUserInteractionOnImage = true // toggle this if user interacts with image

@IBAction func photoButtonAction(sender: UIButton) {
    CameraManager.shared.capture { [weak self] (pixelBuffer, sampleBuffer) in
        self?.handleCameraOutput(pixelBuffer: pixelBuffer, sampleBuffer: sampleBuffer, onFinish: { (image) in
            //...
            DispatchQueue.main.asyncAfter(deadline: .now() + 3) {
                if self?.noUserInteractionOnImage == true {
                    self?.imagePreview.image = nil
                }
            }
        })
    }
}