Skip to content
Snippets Groups Projects
Commit 87499336 authored by 사랑 김's avatar 사랑 김 :military_medal:
Browse files

Upload ViewController.swift

parent b8e01ace
Branches
No related tags found
No related merge requests found
//
// ViewController1.swift
// FOSS_PR
//
// Created by 김사랑 on 2024/06/23.
//
import UIKit
class ViewController: UIViewController {
var collectionView: UICollectionView!
var images: [UIImage] = []
var originalImages: [UIImage] = []
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView()
setupButtons()
}
func setupCollectionView() {
let layout = UICollectionViewFlowLayout()
layout.itemSize = CGSize(width: view.frame.width - 20, height: 300)
layout.scrollDirection = .vertical
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
collectionView.dataSource = self
collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
collectionView.backgroundColor = .white
view.addSubview(collectionView)
}
func setupButtons() {
let downloadButton = createButton(withTitle: "이미지 불러오기", action: #selector(downloadAndProcessImages))
downloadButton.center = CGPoint(x: self.view.center.x, y: self.view.frame.height - 130)
let filterButton = createButton(withTitle: "흑백 필터 적용하기", action: #selector(applyFilterToImages))
filterButton.center = CGPoint(x: self.view.center.x, y: self.view.frame.height - 90)
let resetButton = createButton(withTitle: "되돌리기", action: #selector(resetImages))
resetButton.center = CGPoint(x: self.view.center.x, y: self.view.frame.height - 50)
view.addSubview(downloadButton)
view.addSubview(filterButton)
view.addSubview(resetButton)
}
func createButton(withTitle title: String, action: Selector) -> UIButton {
let button = UIButton(type: .system)
button.setTitle(title, for: .normal)
button.backgroundColor = .lightGray
button.layer.cornerRadius = 10
button.layer.masksToBounds = true
button.titleLabel?.font = UIFont.systemFont(ofSize: 18, weight: .regular)
button.setTitleColor(.black, for: .normal)
button.backgroundColor = .init(red: 0.89, green: 0, blue: 1, alpha: 0.5)
button.frame = CGRect(x: 0, y: 0, width: 170, height: 30)
button.addTarget(self, action: action, for: .touchUpInside)
return button
}
@objc func downloadAndProcessImages() {
loadImagesUsingOperationQueue()
}
@objc func applyFilterToImages() {
applyFilterUsingOperationQueue()
}
@objc func resetImages() {
resetImagesUsingOperationQueue()
}
}
extension ViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
for view in cell.contentView.subviews {
view.removeFromSuperview()
}
let imageView = UIImageView(image: images[indexPath.item])
imageView.frame = cell.contentView.frame
cell.contentView.addSubview(imageView)
return cell
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment