ios - 如何在 Swift 中让 UIImageView 照片在一段时间后消失

标签 ios swift nstimer

我正在开发一个用于分割的相机应用程序,并且我设法捕获实时照片以在 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) {
            
        }
        
    }

最佳答案

您可以使用 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
                }
            }
        })
    }
}

关于ios - 如何在 Swift 中让 UIImageView 照片在一段时间后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62485078/

相关文章:

ios - 将 CGPathRef 转换为 CGMutablePathRef

ios - 确定受 Touch ID 保护的钥匙串(keychain)项是否存在?

ios - 多个计时器一次 ios

ios - 在闭包中使用元类型

ios - 下拉UIViewController暴露一个输入框(文本输入)

objective-c - 在 Swift 中使用一个返回 Optional 或 Throw 的 Objective-C 函数

ios - 订阅 self 是什么意思?

ios - 将函数的槽参数传递为预定计时器的选择器

swift - 为什么我在使用 NSTimer 每 x 秒运行一次函数时收到此错误消息?

iPhone 开发者--performSelector :withObject:afterDelay or NSTimer?