如何在Swift中的时间间隔后使UIImageView照片消失

我正在研究用于分割的相机应用程序,我设法捕获了实时照片以缩略图形式显示在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) {
            
        }
        
    }
iCMS 回答:如何在Swift中的时间间隔后使UIImageView照片消失

您可以使用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
                }
            }
        })
    }
}
本文链接:https://www.f2er.com/2092685.html

大家都在问