1
votes

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!

1

1 Answers

0
votes

First off, check out the ParseUI framework which includes the PFImageView class for automatically handling the downloading and displaying of PFFiles.

Create the outlet

@IBOutlet weak var profilePictureImage: PFImageView!

Typical usage

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

Outside of that, there have been a bunch of posts lately with people experiencing issues with PFFile.getData() not working in Swift2 due to the changes in iOS9 to app transport security. According to Parse this has been fixed in the latest SDK