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