如何使用 UIImagePickerController 相机映像来更新我的 UICollectionViewCell



我需要我的 CollectionViewCell 上的 UIImageView 使用相机图像使用 UIImagePickerController 相机选项进行更新。我认为我所有的渠道和标识符都是正确的。这是我的UIView控制器代码:

import UIKit
import Photos
class CameraVC: BaseViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
@IBAction func importImage(_ sender: Any) {
    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self
    imagePickerController.sourceType = UIImagePickerControllerSourceType.camera
    imagePickerController.allowsEditing = true
    self.present(imagePickerController, animated: true) {
        //After it is complete
    }
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        imageArray.append(image)
    } 
    else {
        //Error
    }
    self.dismiss(animated: true, completion: nil)
    getPhotos()
}
var imageArray = [UIImage]()
func getPhotos() {
    let imgManager = PHImageManager.default()
    let requestOptions = PHImageRequestOptions()
    requestOptions.isSynchronous = true
    requestOptions.deliveryMode = .highQualityFormat
    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
    if let fecthResult: PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions) {
        if fecthResult.count > 0 {
            for i in 0..<fecthResult.count {
                imgManager.requestImage(for: fecthResult.object(at: i) as! PHAsset, targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {
                    image, error in
                    self.imageArray.append(image!)
                })
            }
        }
         else {
            print("You are without any photos.")
            self.collectionView?.reloadData()
        }
    }
}
override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.delegate = self
    collectionView.dataSource = self
    self.addSlideMenuButton()
    self.title = "Camera"
    getPhotos()
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return imageArray.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath)
    let imageView = cell.viewWithTag(1) as! UIImageView
    imageView.image = imageArray[indexPath.row]
    return cell
}
}

这是我的UICollectionViewCell代码:

import UIKit
class CameraImageCell: UICollectionViewCell {
@IBOutlet weak var thumb: UIImageView!    
    func configureCell() { }
}

-感谢您的帮助!

更新以下方法:

func getPhotos() {
let imgManager = PHImageManager.default()
let requestOptions = PHImageRequestOptions()
requestOptions.isSynchronous = true
requestOptions.deliveryMode = .highQualityFormat
let fetchOptions = PHFetchOptions()
fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]
if let fecthResult: PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions) {
    if fecthResult.count > 0 {
        for i in 0..<fecthResult.count {
            imgManager.requestImage(for: fecthResult.object(at: i) as! PHAsset, targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {
                image, error in
                self.imageArray.append(image!)
                self.collectionView?.reloadData()
            })
        }
    }
     else {
        print("You are without any photos.")
    }
}
}

在将图像附加到数组后,您必须重新加载集合视图:

self.collectionView?.reloadData()

将为您提供所需的功能

最新更新