gpt4 book ai didi

ios - 如何使用 UIImagePickerController 相机图像来更新我的 UICollectionViewCell?

转载 作者:行者123 更新时间:2023-11-30 12:43:16 24 4
gpt4 key购买 nike

我需要 CollectionViewCell 上的 UIImageView 使用 UIImagePickerController 相机选项更新相机图像。我认为我所有的 channel 和标识符都是正确的。这是我的 UIViewController 代码:

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.")
}
}
}

将图像附加到数组后,您必须重新加载 Collection View :

self.collectionView?.reloadData()

将为您提供所需的功能

关于ios - 如何使用 UIImagePickerController 相机图像来更新我的 UICollectionViewCell?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41991498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com