Swift 2.0 Parse PFFile / Image

时间:2015-10-01 16:20:07

标签: ios swift parse-platform swift2 pffile

我有一个使用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 an内部UIImage`

我已将项目迁移到Swift 2.0,并且在completion调用时出现未解包的nil错误。

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

1 个答案:

答案 0 :(得分:0)

首先,查看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