【问题标题】:converting PFFile to UIImage with data and append to array使用数据将 PFFile 转换为 UIImage 并附加到数组
【发布时间】:2017-05-07 16:22:04
【问题描述】:

我可以在这个网站上看到几个类似的线程,但是没有一个说明如何从数组中取回数据。

我正在尝试将 PFFile 关闭服务器并将其设置为集合视图中的图像

for object in packs {

      self.packName.append(object["packName"] as! String)
      self.packDescription.append(object["packDesctription"] as! String)

       var objectImage = object["file"] as! PFFile
       objectImage.getDataInBackground(block: { (imageData, error) in

        if error == nil {
           var myImage = UIImage(data: imageData!)
           self.packImage.append(myImage!)
        }
        })

  }

打印 packImage 呈现一堆数据:所以里面有东西..

[<UIImage: 0x608000287210>, {259, 194}]
[<UIImage: 0x608000287210>, {259, 194}, <UIImage: 0x600000287350>, {259, 194}]
[<UIImage: 0x608000287210>, {259, 194}, <UIImage: 0x600000287350>, {259, 194}, <UIImage: 0x6080002870d0>, {249, 192}]
[<UIImage: 0x608000287210>, {259, 194}, <UIImage: 0x600000287350>, {259, 194}, <UIImage: 0x6080002870d0>, {249, 192}, <UIImage: 0x6080002873f0>, {222, 160}]
[<UIImage: 0x608000287210>, {259, 194}, <UIImage: 0x600000287350>, {259, 194}, <UIImage: 0x6080002870d0>, {249, 192}, <UIImage: 0x6080002873f0>, {222, 160}, <UIImage: 0x600000286ea0>, {255, 198}]
[<UIImage: 0x608000287210>, {259, 194}, <UIImage: 0x600000287350>, {259, 194}, <UIImage: 0x6080002870d0>, {249, 192}, <UIImage: 0x6080002873f0>, {222, 160}, <UIImage: 0x600000286ea0>, {255, 198}, <UIImage: 0x608000287670>, {261, 193}]
[<UIImage: 0x608000287210>, {259, 194}, <UIImage: 0x600000287350>, {259, 194}, <UIImage: 0x6080002870d0>, {249, 192}, <UIImage: 0x6080002873f0>, {222, 160}, <UIImage: 0x600000286ea0>, {255, 198}, <UIImage: 0x608000287670>, {261, 193}, <UIImage: 0x600000286cc0>, {259, 194}]
[<UIImage: 0x608000287210>, {259, 194}, <UIImage: 0x600000287350>, {259, 194}, <UIImage: 0x6080002870d0>, {249, 192}, <UIImage: 0x6080002873f0>, {222, 160}, <UIImage: 0x600000286ea0>, {255, 198}, <UIImage: 0x608000287670>, {261, 193}, <UIImage: 0x600000286cc0>, {259, 194}, <UIImage: 0x608000287800>, {189, 267}]
[<UIImage: 0x608000287210>, {259, 194}, <UIImage: 0x600000287350>, {259, 194}, <UIImage: 0x6080002870d0>, {249, 192}, <UIImage: 0x6080002873f0>, {222, 160}, <UIImage: 0x600000286ea0>, {255, 198}, <UIImage: 0x608000287670>, {261, 193}, <UIImage: 0x600000286cc0>, {259, 194}, <UIImage: 0x608000287800>, {189, 267}, <UIImage: 0x600000287620>, {259, 194}]

但是,当我尝试将图像设置得更低时,我得到一个错误索引超出范围。

cell.imageCell.image = packImage[indexPath.row]

---------编辑 UICollectionViewDelegate 的附加完整代码----------------

import UIKit
import Parse

private let reuseIdentifier = "Cell"

class PackViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate  {

    //have to drag in outlet because not in CVC anymore
    @IBOutlet var collectionView: UICollectionView!

    //var delegate: PackViewDelegate?

    var packName = [""]  // the total packs in the app
    var packDescription = [""]
    var packTitle = [""]
    var packImage = [UIImage]()

    override func viewDidLoad() {
        super.viewDidLoad()


        let packQuery = PFQuery(className: "Pack")

        packQuery.findObjectsInBackground(block: { (objectsArray, error) in
            if error != nil {
                print(error!)
            } else if let packs = objectsArray {

                self.packName.removeAll() // remove the empty strings
                //self.packImage.removeAll()
                self.packDescription.removeAll()

                for object in packs {

                    self.packName.append(object["packName"] as! String)
                    self.packDescription.append(object["packDesctription"] as! String)

                    // get the PFFile from the server
                    var objectImage = object["file"] as! PFFile
                    objectImage.getDataInBackground(block: { (imageData, error) in

                        if error == nil {
                            //convert the data to an image and append it to array
                            var myImage = UIImage(data: imageData!)
                            self.packImage.append(myImage!)
                            print(self.packImage)
                        }
                    })

                }

                //only works when create iboutlet of collection view because in VC not CVC
                self.collectionView?.reloadData()

            }
        })

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }


    // MARK: UICollectionViewDataSource

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }


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


    func UIColorFromHEX(hexValue: UInt) -> UIColor {
        return UIColor(
            red: CGFloat((hexValue & 0xFF0000) >> 16) / 255.0,
            green: CGFloat((hexValue & 0x00FF00) >> 8) / 255.0,
            blue: CGFloat(hexValue & 0x0000FF) / 255.0,
            alpha: CGFloat(1.0)
        )
    }


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


        cell.labelCell.text = packDescription[indexPath.row]

//////-------------------------------------------------------------- this is where it crashes
        cell.imageCell.image = packImage[indexPath.row]



        cell.imageCell.layer.masksToBounds = true
        cell.imageCell.layer.cornerRadius = cell.imageCell.frame.height/2

        cell.imageCell.layer.borderWidth = 3
        cell.imageCell.layer.borderColor = UIColorFromHEX(hexValue: 0x62aca2).cgColor

        return cell

    }


    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

            // handle the segue to JourneyViewController with variable "selectedPack"
            // not sure why you need to set the storyboard but it works
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
            //create instance of the viewcontroller
        let transportJourneyViewController = storyBoard.instantiateViewController(withIdentifier: "JourneyViewController") as! JourneyViewController
            //value to pass - has been defined in journey
        transportJourneyViewController.selectedPack = packName[indexPath.row]
            //present the vc
        self.present(transportJourneyViewController, animated:true, completion:nil)

    }


    @IBAction func logoutButtonTapped(_ sender: Any) {
        PFUser.logOut()
        if (PFUser.current() == nil) {
            performSegue(withIdentifier: "segueLogout", sender: self)
        } else {
            MyFunctions.createAlert(errorTitle: "Oops...", errorMessage: "Something happened when logging you out.", className: self)
        }
    }

} // class

【问题讨论】:

  • 在您的问题中添加 Tableview 委托方法 numberOfRowsInSection 的代码?
  • 很奇怪,有 9 个单元格,但这个数组超出了 9 个项目的范围?我确定它与将文件从数据转换为 UIImage 但看不到它有关。
  • 你试过我的答案了吗?
  • 我不清楚这个答案的含义?

标签: ios swift uiimageview uiimage pffile


【解决方案1】:

我认为您的array 有问题。你的

packName.count > self.packImage.count

使用这个数组self.packImagenumberOfItemsInSectionto方法来消除崩溃

试试这个

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

希望对你有帮助

【讨论】:

  • Yes 消除了崩溃。仍然无法使用 cell.imageCell.image = packImage[indexPath.row] 显示图像
  • 只要注释掉这行 cell.imageCell.layer.cornerRadius = cell.imageCell.frame.height/2 显示图片的话我们再讨论
  • 自从更改为 self.packImage.count 后,没有其他数据显示,如 packDescription 或边框等。当注释掉圆角半径 2 项目时显示所有边框,描述等但应该是 9?
  • 告诉我 packDescription.count 和 packImage.count 吗?
  • packDescription = 9, packImage = 1 这次只显示1张图片。
【解决方案2】:
indexPath.row

标识表视图部分中的行的索引号。

indexPath.item

标识集合视图部分中的项目的索引号。

使用cell.imageCell.image = packImage[indexPath.item] 而不是cell.imageCell.image = packImage[indexPath.row],因为它是一个集合视图。并检查集合视图数据源中的 numberOfItemsInSection 是否小于或等于您的 packDescription 数组 packImage。

【讨论】:

  • 感谢您指出 .item。不幸的是,问题似乎在于从 PFFile 到 UIImage 的转换
猜你喜欢
  • 1970-01-01
  • 2015-07-30
  • 1970-01-01
  • 2015-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-08
  • 1970-01-01
相关资源
最近更新 更多