【问题标题】:Managing profile picture of subclassed PFUser with PFFile - Swift / iOS使用 PFFile 管理子类 PFUser 的个人资料图片 - Swift / iOS
【发布时间】:2015-06-23 18:50:34
【问题描述】:

我在我的 iOS 应用程序中对 PFUser 进行了子类化,并且我正在使用此函数来获取个人资料图片。 profilePicture@NSManaged PFFileprofilePictureImage 是 UIImage。

这很好用,除了 getData()fetchIfNeeded() 是主线程上潜在的长时间运行操作。

谁能想到一个好方法来实现这个方法,让可怕的部分在后台线程上运行?

谢谢!

func image() -> UIImage!
{
    if !(self.profilePictureImage != nil)
    {
        if self.profilePicture != nil
        {
            self.fetchIfNeeded()
            if let data = self.profilePicture!.getData() {
                self.profilePictureImage = UIImage(data: data)
                return self.profilePictureImage
            }
        }else {
            return UIImage(named: "no_photo")!
        }
    }
    return self.profilePictureImage
}

【问题讨论】:

    标签: ios swift parse-platform pfuser pffile


    【解决方案1】:

    更改方法,以便在图像可用时调用闭包并将其作为参数传递,而不是返回图像。如果需要下载图像,可以立即调用,也可以延迟一段时间后调用。

    【讨论】:

      【解决方案2】:

      照你说的做,在后台运行任务:fetchIfNeededInBackgroundWithBlock。此外,您的 image 函数应如下所示:

      func imageInBackgroundWithBlock(block: ((UIImage?, NSError?) -> Void)?) {
          var image: UIImage?
      
          self.fetchIfNeededInBackgroundWithBlock({ (user, error) -> Void in 
              if error == nil {
                  // load the picture here
                  image = ...
      
              } else {
                  println(error!.userInfo)
                  image = UIImage(named: "no_photo")!
              }
      
              // return after fetching the user and the image
              block?(image, error)
          })
      
      }
      

      【讨论】:

        猜你喜欢
        • 2017-09-30
        • 1970-01-01
        • 1970-01-01
        • 2018-09-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多