gpt4 book ai didi

ios - 为什么我的图像没有显示在 UIImageView 中?

转载 作者:行者123 更新时间:2023-11-29 05:42:12 38 4
gpt4 key购买 nike

我想要一个 if 语句,如果所选按钮对应于第一个 ImageView ,则将其设置为第一个图像,否则将其设置为第二个 ImageView ...但是,一旦我从图像选择器,它只是忽略它并继续前进,就像什么都没发生一样。这是我的代码:(它位于图像选择器 Controller 功能下...)

class UploadSubPostCell: UICollectionViewCell {
@IBOutlet weak var previewStep: UIImageView!
}

class UploadViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

@IBOutlet weak var previewImage: UIImageView!
@IBOutlet weak var postBtn: UIButton!
@IBOutlet weak var selectBtn: UIButton!

@IBOutlet weak var postscollectionview: UICollectionView!
@IBOutlet weak var selectStepsBtn: UIButton!

var picker = UIImagePickerController()
var isThumbnailImage = true
var subpostsArray = [UIImage]()
var subposts = [SubPost]()
var posts = [Post]()

override func viewDidLoad() {
super.viewDidLoad()

setupNavigationBarItems()
picker.delegate = self
}

func setupNavigationBarItems() {
navigationItem.title = "Upload"
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let image = info[UIImagePickerControllerEditedImage] as? UIImage {

let path = IndexPath(item: 0, section: 0)
let cell = self.postscollectionview.cellForItem(at: path) as? UploadSubPostCell

if isThumbnailImage{
previewImage.image = image
} else {
cell?.previewStep.image = image
}
selectBtn.isHidden = false
selectStepsBtn.isHidden = false
postBtn.isHidden = false
if isThumbnailImage == false {
subpostsArray.append(image)
subposts.count + 1
print("Appended image to array:", subpostsArray)
}
}
picker.dismiss(animated: true, completion: nil)
}

@IBAction func selectStepPressed(_ sender: Any) {

picker.allowsEditing = true
picker.sourceType = .photoLibrary
isThumbnailImage = false

self.present(picker, animated: true, completion: nil)
}


@IBAction func selectPressed(_ sender: Any) {

picker.allowsEditing = true
picker.sourceType = .photoLibrary
isThumbnailImage = true

self.present(picker, animated: true, completion: nil)
}

@IBAction func addNewPressed(_ sender: Any) {
}


@IBAction func postPressed(_ sender: Any) {
AppDelegate.instance().showActivityIndicator()

let uid = Auth.auth().currentUser!.uid
let ref = Database.database().reference()
let storage = Storage.storage().reference(forURL: "gs://mobile-d9fcd.appspot.com")

let key = ref.child("posts").childByAutoId().key
let imageRef = storage.child("posts").child(uid).child("\(key).jpg")
let data = UIImageJPEGRepresentation(self.previewImage.image!, 0.6)

let uploadTask = imageRef.putData(data!, metadata: nil) { (metadata, error) in
if error != nil {
print(error!.localizedDescription)
AppDelegate.instance().dismissActivityIndicator()
return
}
imageRef.downloadURL(completion: { (url, error) in
if let url = url {
let feed = ["userID" : uid,
"pathToImage" : url.absoluteString,
"likes" : 0,
"author" : Auth.auth().currentUser!.displayName!,
"postID" : key] as [String : Any]

let postFeed = ["\(key)" : feed]

ref.child("posts").updateChildValues(postFeed)
AppDelegate.instance().dismissActivityIndicator()
self.picker.dismiss(animated: true, completion: nil)
}
})
}
uploadTask.resume()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.posts.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "postCell", for: indexPath) as! PostCell

cell.postImage.downloadImage(from: self.posts[indexPath.row].pathToImage)
cell.authorLabel.text = self.posts[indexPath.row].author
cell.likeLabel.text = "\(self.posts[indexPath.row].likes!) Likes"
cell.postID = self.posts[indexPath.row].postID
}
}
}

最佳答案

didFinishPickingMediaWithInfo中,如果您知道cell是什么,您应该使用dataSource保存图像。 strong>image 进入,使用 tableView.reloadRows(at indexPaths:) 重新加载单元格。将图像添加到cellForRowAt()

中的 cell.previewStep.image

关于ios - 为什么我的图像没有显示在 UIImageView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56452789/

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