自作アプリを作ってみた -アプリ名:hanagasaki- video
はじめに
自作アプリを作りましたので、画面毎の機能とコードを説明したいと思います。
今回、video 画面の機能とコードを説明が記載されています。
使用環境
● OS:macOS Big Sur 11.3.1
● Xcode:12.5
● Swift:5.4
● DB Browser for SQLite:3.12.1
コード
// VideoViewController.swift
import UIKit
class VideoViewController: BaseViewController {
@IBOutlet weak var videoCollectionView: UICollectionView!
var modelArray = [modelVideo]()
var vc = VideoEntryViewController()
let cell = VideoCell()
override func viewDidLoad() {
super.viewDidLoad()
videoCollectionView.delegate = self
videoCollectionView.dataSource = self
addCustomCell()
layoutCollectionView()
addHeaderBar("Video",
UIBarButtonItem(image: #imageLiteral(resourceName: "bars_24"), style: .done, target: self, action: #selector(tapHambugerMenu)),
UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(tapAddVideo)))
}
override func viewWillAppear(_ animated: Bool) {
modelArray = ModelManager.getInstance().getAllVideo()
videoCollectionView.reloadData()
}
func addCustomCell() {
let customCell = UINib(nibName: "VideoCell", bundle: nil)
videoCollectionView.register(customCell, forCellWithReuseIdentifier: "VideoCell")
}
func layoutCollectionView() {
let layout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15)
layout.itemSize = CGSize(width: 300, height: 300)
videoCollectionView.collectionViewLayout = layout
}
}
extension VideoViewController: UICollectionViewDelegate,UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return modelArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "VideoCell", for: indexPath) as! VideoCell
cell.number.text = modelArray[indexPath.row].videoTitle
let base64String = modelArray[indexPath.row].image!
let imageData = NSData(base64Encoded: base64String, options: .ignoreUnknownCharacters)
if imageData == imageData {
let image = UIImage(data: imageData! as Data)
cell.imageVideo.image = image
}
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let horizontalSpace : CGFloat = 20
let cellSize : CGFloat = self.view.bounds.width / 3 - horizontalSpace
return CGSize(width: cellSize, height: cellSize)
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let url = URL(string: modelArray[indexPath.row].url!)
if UIApplication.shared.canOpenURL(url!) {
UIApplication.shared.open(url!)
}
}
}
// VideoEntryViewController.swift
import UIKit
class VideoEntryViewController: BaseViewController, UITextFieldDelegate {
@IBOutlet weak var selectImageBtn: UIButton! {
didSet {
selectImageBtn.layer.cornerRadius = 75
}
}
@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var urlLabel: UILabel!
@IBOutlet weak var titleTextField: UITextField! {
didSet {
titleTextField.backgroundColor = .systemFill
titleTextField.placeholder = "Add title ."
}
}
@IBOutlet weak var urlTextField: UITextField! {
didSet {
urlTextField.backgroundColor = .systemFill
urlTextField.placeholder = "Add url ."
}
}
@IBOutlet weak var entryButton: UIButton! {
didSet {
entryButton.layer.cornerRadius = 10
}
}
override func viewDidLoad() {
super.viewDidLoad()
addHeaderBar("VideoEntry", nil, nil)
tapEnable()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
@IBAction func tapUploadImage(_ sender: UIButton) {
/*if isEdit == true {
updateImageData()
}*/
print("tappedProfileImageButton")
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true
self.present(imagePickerController, animated: true, completion: nil)
}
func tapEnable() {
entryButton.isEnabled = false
entryButton.backgroundColor = .black.withAlphaComponent(0.15)
}
@IBAction func TextFieldActionBtnInactive(_ sender: Any) {
if titleTextField.text == "" {
entryButton.isEnabled = false
entryButton.backgroundColor = .black.withAlphaComponent(0.15)
}
else if urlTextField.text == "" {
entryButton.isEnabled = false
entryButton.backgroundColor = .black.withAlphaComponent(0.15)
} else {
entryButton.isEnabled = true
entryButton.backgroundColor = UIColor(hex: 0xFF19FDD3)
}
}
@IBAction func TextFieldActionBtnInactive1(_ sender: Any) {
if urlTextField.text == "" {
entryButton.isEnabled = false
entryButton.backgroundColor = .black.withAlphaComponent(0.15)
} else if titleTextField.text == "" {
entryButton.isEnabled = false
entryButton.backgroundColor = .black.withAlphaComponent(0.15)
} else {
entryButton.isEnabled = true
entryButton.backgroundColor = UIColor(hex: 0xFF19FDD3)
}
}
@IBAction func videoEntry(_ sender: UIButton) {
//TODO: base64に変化
//let base64 =
//let decode =
let selectVideoImage = (selectImageBtn.imageView?.image!.pngData()!)! as NSData
let base64String = selectVideoImage.base64EncodedString(options: .lineLength64Characters)
print(base64String)
let video = modelVideo(id: "", videoTitle: titleTextField.text, image: base64String, url: urlTextField.text)
let isSave = ModelManager.getInstance().SaveVideo(video: video)
dismiss(animated: true, completion: nil)
print("isSave : -\(isSave)")
}
}
extension VideoEntryViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let editingImage = info[.editedImage] as? UIImage {
selectImageBtn.setImage(editingImage.withRenderingMode(.alwaysOriginal), for: .normal) } else if let originalImage = info[.originalImage] as? UIImage {
selectImageBtn.setImage(originalImage.withRenderingMode(.alwaysOriginal), for: .normal)
}
selectImageBtn.setTitle("", for: .normal)
selectImageBtn.imageView?.contentMode = .scaleAspectFill
selectImageBtn.contentHorizontalAlignment = .fill
selectImageBtn.contentVerticalAlignment = .fill
selectImageBtn.clipsToBounds = true
dismiss(animated: true, completion: nil)
}
}
// VideoCell.swift
import UIKit
class VideoCell: UICollectionViewCell {
@IBOutlet weak var number: UILabel!
@IBOutlet weak var imageVideo: UIImageView! {
didSet {
imageVideo.layer.cornerRadius = 150
}
}
}
おわりに
最後まで読んで下さりありがとうございます。
次回は、qiita画面の記事を投稿したいと思います。
この記事が何かの実装に参考になれば幸いです。
参考文献
この記事が気に入ったらサポートをしてみませんか?