Ios 如何使用UIImagePickerController相机图像更新UICollectionViewCell?
我需要CollectionViewCell上的UIImageView使用UIImagePickerController相机选项更新相机图像。我认为我所有的出口和标识都是正确的。以下是我的UIViewController代码:Ios 如何使用UIImagePickerController相机图像更新UICollectionViewCell?,ios,iphone,swift,xcode,uicollectionview,Ios,Iphone,Swift,Xcode,Uicollectionview,我需要CollectionViewCell上的UIImageView使用UIImagePickerController相机选项更新相机图像。我认为我所有的出口和标识都是正确的。以下是我的UIViewController代码: import UIKit import Photos class CameraVC: BaseViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate, UICollec
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
}
}
-谢谢你的帮助 更新以下方法:
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.")
}
}
}
将为您提供所需的功能
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()