【问题标题】:Swift 2.0 Parse PFFile/ImageSwift 2.0 解析 PFFile/图像
【发布时间】:2015-12-29 18:55:57
【问题描述】:

我有一个使用 Parse 存储个人资料图片的 Swift 项目。出于某种原因,PFFile 配置文件图像很难开始工作。我终于用这个功能让它在 Swift 1.2 中工作了:

func image(completion: (image: UIImage) -> Void)
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), {

        if self.profilePictureImage == nil
        {
            if self.profilePicture != nil
            {

                self.fetchIfNeeded()
                if let data = self.profilePicture!.getData()
                {
                    self.profilePictureImage = UIImage(data: data)

                }
            }else
            {
                self.profilePictureImage = UIImage(named: "no_photo")!
            }
        }

        dispatch_async(dispatch_get_main_queue(),{

            completion(image: self.profilePictureImage)

        })
    })
}

profilePicture@NSManaged PFFile

profilePictureImage' is aninternal UIImage`

我已将项目迁移到 Swift 2.0,但它在 completion 调用时因 unwrapped nil 错误而崩溃。

发生了什么变化?我该如何解决这个问题?谢谢!

【问题讨论】:

    标签: ios swift parse-platform swift2 pffile


    【解决方案1】:

    首先,查看ParseUI 框架,其中包含PFImageView 类,用于自动处理PFFiles 的下载和显示。

    创建出口

    @IBOutlet weak var profilePictureImage: PFImageView!
    

    典型用法

    // Set placeholder image
    profilePictureImage.image = UIImage(named: "no_photo")
    
    // Set remote image (PFFile)
    profilePictureImage.file = profilePicture
    
    // Once the download completes, the remote image will be displayed
    profilePictureImage.loadInBackground { (image: UIImage?, error: NSError?) -> Void in
        if (error != nil) {
            // Log details of the failure
            println("Error: \(error!) \(error!.userInfo!)")
        } else {
            // profile picture loaded
        }
    }
    

    除此之外,由于 iOS9 对应用程序传输安全性的更改,最近有很多帖子显示PFFile.getData() 无法在 Swift2 中工作。 According to Parse this has been fixed in the latest SDK

    【讨论】:

      猜你喜欢
      • 2016-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      相关资源
      最近更新 更多