1

I have a Swift project that uses Parse to store profile pics. For some reason the PFFile profile image was a pain to get working. I finally got it working in Swift 1.2 with this function:

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 is the @NSManaged PFFile

profilePictureImage' is aninternal UIImage`

I've migrated the project to Swift 2.0 and it's crashing with an unwrapped nil error on the completion call.

What's changed? How can I address this? Thanks!

4

1 回答 1

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
    }
}

PFFile.getData()除此之外,由于 iOS9 对应用程序传输安全性的更改,最近有很多人遇到无法在 Swift2 中工作的问题。根据 Parse,这已在最新的 SDK 中修复

于 2015-10-01T16:34:46.623 回答