UIImage 和 UIImageView show/hide 问题
UIImage and UIImageView show/hide problems
我在显示和隐藏我的应用程序的 UIImage 和 UIImageView 的代码时遇到问题。我希望按钮从 AVCapture 会话中捕获照片,然后在第一次按下时使照片成为屏幕的整个视图。然后,当用户再次点击按钮时,它会删除 UIImage 和 UIIamgeView。
这是我目前的代码
extension ViewController: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto
photo: AVCapturePhoto, error: Error?) {
guard let data = photo.fileDataRepresentation() else{
return }
let image = UIImage(data: data)
let Photo = UIImageView(image: image)
if shutterButton.tag == 0 {
shutterButton.tag = 1
Photo.contentmode = .scaleAspectFill
view.insertSubview(Photo, belowSubview: shutterbutton)
}
else if shutterButton.tag == 1{
shutterButton.tag = 0 }
我已尝试使用(下方)作为可能的修复方法,但这并没有解决我的问题
.isHidden = true
我该怎么做才能解决我的问题?
好的 - 你在评论中说 “快门按钮是通过代码添加的”,所以你的代码大概是这样的:
class ViewController: UIViewController {
let shutterButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
// other stuff
view.addSubview(shutterButton)
// constraints for shutterButton and other elements
}
}
然后,当你得到照片图像时,你正在这样做:
let image = UIImage(data: data)
let Photo = UIImageView(image: image)
if shutterButton.tag == 0 {
shutterButton.tag = 1
Photo.contentMode = .scaleAspectFill
view.insertSubview(Photo, belowSubview: shutterButton)
}
else if shutterButton.tag == 1
{
shutterButton.tag = 0
}
这意味着您正在创建一个 NEW 图像视图并将其作为子视图插入 每次这是叫.
如果你想在点击 every-other 按钮时“添加照片”/“删除照片”,你希望图像视图已经存在,这样你就可以设置图像并显示或隐藏图像视图而不是创建新图像。
因此,以与添加按钮相同的方式添加图像视图...但在添加按钮之前添加它(这样按钮将“位于顶部”) , 并将其设置为隐藏以开始:
class ViewController: UIViewController {
let shutterButton = UIButton()
let photoImageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
// other stuff
// setup the photo image view
photoImageView.contentMode = .scaleAspectFill
// start with it hidden
photoImageView.isHidden = true
// add the photo image view to the view hierarchy
view.addSubview(photoImageView)
// add the button last
view.addSubview(shutterButton)
// constraints for the image view
// constraints for shutterButton and other elements
}
}
那么,当你处理拍照时:
extension ViewController: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto
photo: AVCapturePhoto, error: Error?) {
guard let data = photo.fileDataRepresentation() else {
return
}
let image = UIImage(data: data)
// don't do this
//let Photo = UIImageView(image: image)
if shutterButton.tag == 0 {
shutterButton.tag = 1
// don't do this
//Photo.contentMode = .scaleAspectFill
//view.insertSubview(Photo, belowSubview: shutterButton)
// instead, do this
photoImageView.image = image
photoImageView.isHidden = false
}
else if shutterButton.tag == 1
{
shutterButton.tag = 0
// do this to "remove" the image
photoImageView.image = nil
photoImageView.isHidden = true
}
}
}
我在显示和隐藏我的应用程序的 UIImage 和 UIImageView 的代码时遇到问题。我希望按钮从 AVCapture 会话中捕获照片,然后在第一次按下时使照片成为屏幕的整个视图。然后,当用户再次点击按钮时,它会删除 UIImage 和 UIIamgeView。
这是我目前的代码
extension ViewController: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto
photo: AVCapturePhoto, error: Error?) {
guard let data = photo.fileDataRepresentation() else{
return }
let image = UIImage(data: data)
let Photo = UIImageView(image: image)
if shutterButton.tag == 0 {
shutterButton.tag = 1
Photo.contentmode = .scaleAspectFill
view.insertSubview(Photo, belowSubview: shutterbutton)
}
else if shutterButton.tag == 1{
shutterButton.tag = 0 }
我已尝试使用(下方)作为可能的修复方法,但这并没有解决我的问题
.isHidden = true
我该怎么做才能解决我的问题?
好的 - 你在评论中说 “快门按钮是通过代码添加的”,所以你的代码大概是这样的:
class ViewController: UIViewController {
let shutterButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
// other stuff
view.addSubview(shutterButton)
// constraints for shutterButton and other elements
}
}
然后,当你得到照片图像时,你正在这样做:
let image = UIImage(data: data)
let Photo = UIImageView(image: image)
if shutterButton.tag == 0 {
shutterButton.tag = 1
Photo.contentMode = .scaleAspectFill
view.insertSubview(Photo, belowSubview: shutterButton)
}
else if shutterButton.tag == 1
{
shutterButton.tag = 0
}
这意味着您正在创建一个 NEW 图像视图并将其作为子视图插入 每次这是叫.
如果你想在点击 every-other 按钮时“添加照片”/“删除照片”,你希望图像视图已经存在,这样你就可以设置图像并显示或隐藏图像视图而不是创建新图像。
因此,以与添加按钮相同的方式添加图像视图...但在添加按钮之前添加它(这样按钮将“位于顶部”) , 并将其设置为隐藏以开始:
class ViewController: UIViewController {
let shutterButton = UIButton()
let photoImageView = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
// other stuff
// setup the photo image view
photoImageView.contentMode = .scaleAspectFill
// start with it hidden
photoImageView.isHidden = true
// add the photo image view to the view hierarchy
view.addSubview(photoImageView)
// add the button last
view.addSubview(shutterButton)
// constraints for the image view
// constraints for shutterButton and other elements
}
}
那么,当你处理拍照时:
extension ViewController: AVCapturePhotoCaptureDelegate {
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto
photo: AVCapturePhoto, error: Error?) {
guard let data = photo.fileDataRepresentation() else {
return
}
let image = UIImage(data: data)
// don't do this
//let Photo = UIImageView(image: image)
if shutterButton.tag == 0 {
shutterButton.tag = 1
// don't do this
//Photo.contentMode = .scaleAspectFill
//view.insertSubview(Photo, belowSubview: shutterButton)
// instead, do this
photoImageView.image = image
photoImageView.isHidden = false
}
else if shutterButton.tag == 1
{
shutterButton.tag = 0
// do this to "remove" the image
photoImageView.image = nil
photoImageView.isHidden = true
}
}
}