How to Implement ImagePlaygroundViewController in swift?

ImagePlaygroundViewController is not a standard or predefined class in iOS development. However, it sounds like a custom UIViewController created for a specific purpose, possibly to display and manipulate images within an iOS application. Below is a breakdown of how such a view controller could be structured and implemented in Swift:



Code : 

import UIKit

class ImagePlaygroundViewController: UIViewController {
    
    // MARK: - Properties
    var imageView: UIImageView!
    var selectedImage: UIImage?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    // MARK: - Setup UI
    private func setupUI() {
        view.backgroundColor = .white
        
        // Image View
        imageView = UIImageView(frame: view.bounds)
        imageView.contentMode = .scaleAspectFit
        imageView.isUserInteractionEnabled = true
        if let image = selectedImage {
            imageView.image = image
        }
        view.addSubview(imageView)
        
        // Add gesture recognizers
        addGestures()
        
        // Add toolbar
        setupToolbar()
    }
    
    private func setupToolbar() {
        let toolbar = UIToolbar()
        toolbar.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(toolbar)
        
        NSLayoutConstraint.activate([
            toolbar.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
            toolbar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            toolbar.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            toolbar.heightAnchor.constraint(equalToConstant: 50)
        ])
        
        let saveButton = UIBarButtonItem(barButtonSystemItem: .save, target: self, action: #selector(saveImage))
        let shareButton = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareImage))
        toolbar.setItems([saveButton, UIBarButtonItem.flexibleSpace(), shareButton], animated: false)
    }
    
    // MARK: - Gesture Recognizers
    private func addGestures() {
        let pinchGesture = UIPinchGestureRecognizer(target: self, action: #selector(handlePinch(_:)))
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(handlePan(_:)))
        
        imageView.addGestureRecognizer(pinchGesture)
        imageView.addGestureRecognizer(panGesture)
    }
    
    @objc private func handlePinch(_ sender: UIPinchGestureRecognizer) {
        guard let view = sender.view else { return }
        view.transform = view.transform.scaledBy(x: sender.scale, y: sender.scale)
        sender.scale = 1.0
    }
    
    @objc private func handlePan(_ sender: UIPanGestureRecognizer) {
        let translation = sender.translation(in: view)
        if let gestureView = sender.view {
            gestureView.center = CGPoint(x: gestureView.center.x + translation.x,
                                         y: gestureView.center.y + translation.y)
        }
        sender.setTranslation(.zero, in: view)
    }
    
    // MARK: - Toolbar Actions
    @objc private func saveImage() {
        guard let image = imageView.image else { return }
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
        let alert = UIAlertController(title: "Saved", message: "Image saved to Photos.", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
        present(alert, animated: true, completion: nil)
    }
    
    @objc private func shareImage() {
        guard let image = imageView.image else { return }
        let activityVC = UIActivityViewController(activityItems: [image], applicationActivities: nil)
        present(activityVC, animated: true, completion: nil)
    }
}
Use code : 

let imagePlaygroundVC = ImagePlaygroundViewController()
imagePlaygroundVC.selectedImage = UIImage(named: "exampleImage")
navigationController?.pushViewController(imagePlaygroundVC, animated: true)

How to Implement ImagePlaygroundViewController in swift? How to Implement ImagePlaygroundViewController in swift? Reviewed by KIRIT MODI on 20:13:00 Rating: 5

No comments:

Powered by Blogger.