【发布时间】:2017-05-13 11:37:57
【问题描述】:
代码是用 Swift 编写的。我正在构建一个用户可以发布帖子的社交应用程序。我使用 Firebase 作为后端(数据库、存储)。所以,我有一个UICollectionView,它从设备的照片库中获取所有照片,并使用自定义单元格填充集合视图。在同一个视图控制器中,我有另一个自定义单元格,用户可以使用它来拍照并使用它来发帖。为了更清楚:
如果用户决定拍照,当他们点击“使用照片”时,他们需要被呈现给一个新的视图控制器,该控制器应该显示他们刚刚拍摄的照片以及其他选项(例如标题、描述和标签使用
UITextFields&UITextView)。如果用户决定从他们自己的库中选择多张照片,我必须以某种方式标记这些照片/单元格(即使用一个复选标记按钮),将所选照片添加到数组中(有一些限制,可能是 10 张照片顶部)。当他们单击“下一步”按钮时,需要将数组发送到新的 Post View Controller,其中所有图像都应该动态显示,可能使用水平的
UICollectionView(?!)(如果是,可以选择删除图像意外选择),同样,如上所述,有机会添加标题、描述等。现在,我不知道该怎么做。
我正在寻找一个解决方案,但我现在有点卡住了这几天,所以非常欢迎帮助!
这是我在 Collection View 控制器中的内容(PS:我没有包含从照片中获取图像的功能的部分)
import UIKit
import Photos
class PrePhotoPostVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var nextButton: UIBarButtonItem!
var photosLibraryArray = [UIImage]()
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
checkPhotoLibraryPermission()
setupCollectionViewDelegates()
}
@IBAction func cancelButtonPressed (_ sender: UIBarButtonItem) {
dismiss(animated: true, completion: nil)
}
@IBAction func nextButtonPressed (_ sender: UIBarButtonItem) {
nextButton.isEnabled = false
}
@IBAction func takeAphotoButtonPressed (_ sender: UIButton) {
// Camera Autorization
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo) { response in
if response {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
else {
print("Camera isn't available in similator")
}
}
else {
print("unautorized")
}
}
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 2
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if section == 0 {
return 1
} else {
return photosLibraryArray.count
}
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.section == 0 {
let cellCamera = collectionView.dequeueReusableCell(withReuseIdentifier: cellPrePostCameraCell, for: indexPath)
return cellCamera
}
else {
let cellPhotoLibrary = collectionView.dequeueReusableCell(withReuseIdentifier: cellPrePostPhotoLibrary, for: indexPath) as! PrePhotoPostPhotoLIbraryCell
cellPhotoLibrary.awakeFromNib()
cellPhotoLibrary.photoLibraryImage.image = photosLibraryArray[indexPath.row]
return cellPhotoLibrary
}
}
}
UICollectionView 的截图如下:
这是我在照片库单元格中的代码:
import UIKit
class PrePhotoPostPhotoLIbraryCell: UICollectionViewCell {
// MARK: Outlets
@IBOutlet weak var photoLibraryImage: UIImageView!
// var selectedPhotos = [UIImageView]()
@IBAction func selectedButtonPressed(_ sender: UIButton) {
self.layer.borderWidth = 3.0
self.layer.borderColor = isSelected ? UIColor.blue.cgColor : UIColor.clear.cgColor
}
override func awakeFromNib() {
photoLibraryImage.clipsToBounds = true
photoLibraryImage.contentMode = .scaleAspectFill
photoLibraryImage.layer.borderColor = UIColor.clear.cgColor
photoLibraryImage.layer.borderWidth = 1
photoLibraryImage.layer.cornerRadius = 5
}
}
【问题讨论】:
标签: swift uiimageview uicollectionview uiimage data-transfer