I am having an issue with code to show and hide a UIImage and UIImageView for my app. I want the button to capture a photo from the AVCapture session and then make the photo the entire view of the screen on the first press. Then when the user hits the button again it removes the UIImage and UIIamgeView.
Here is the code I have so far
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 }
I have tried using (below) as a possible fix but that does no resolve my issue
.isHidden = true
What can I do to resolve my problem?
CodePudding user response:
OK - you said in the comments that "The shutter button was added via code", so presumably your code looks something like this:
class ViewController: UIViewController {
let shutterButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
// other stuff
view.addSubview(shutterButton)
// constraints for shutterButton and other elements
}
}
then, when you get the photo image, you're doing this:
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
}
which means you're creating a NEW image view and inserting it as a subview every time this is called.
If you want to "add a photo" / "remove the photo" on every-other button tap, you want to have your image view already there, so you can set the image and show or hide the image view instead of creating new ones.
So, add your image view the same way you added the button... but add it before you add the button (so the button will be "on top"), and set it to hidden to start with:
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
}
}
then, when you handle the photo capture:
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
}
}
}