Search code examples
iosswiftparse-platformuiimage

Swift - Display two separate images in 2 UIImageViews in 1 ViewController


I would like to display two images on one viewcontroller selecting them from the camera roll,

I have managed to get to the camera roll to be able to select them but when I select one image it places them in both UIImageViews.

I want to be able to display two different images in the UIImageView.

 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        //handle media here i.e. do stuff with photo

        print("imagePickerController called")


        if let chosenImage = info[UIImagePickerControllerEditedImage] {
        profileImage.image = chosenImage as? UIImage

        let user: PFUser = PFUser.currentUser()!
        let profileImageData = UIImageJPEGRepresentation(profileImage.image!, 1)
        if (profileImageData != nil)
        {
            let profileFileObject = PFFile(data: profileImageData!)
            user.setObject(profileFileObject!, forKey: "profilePicture")
            }
        }

Up to here I can select one image and save it to Parse successfully, but when I try and add the other code for the other UIImageView, like this:

            if let chosenCoverImage = info[UIImagePickerControllerEditedImage] {
            coverImage.image = chosenCoverImage as? UIImage

            let coverUser: PFUser = PFUser.currentUser()!
            let coverImageData = UIImageJPEGRepresentation(coverImage.image!, 1)
            if (coverImageData != nil)
            {
                let coverFileObject = PFFile(data: coverImageData!)
                coverUser.setObject(coverFileObject!, forKey: "coverPicture")
                }}
            }

That second part of code doesn't seem to work though, basically it shows the same image in both UIImageViews, what I think I need is a second

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
}

But that oviously isn't possible in the same swift file.

Any idea's on how I could achieve this?

Thank you in advance!

Here's my full imagePickerConytoller code:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        //handle media here i.e. do stuff with photo

        print("imagePickerController called")


        let chosenImage = info[UIImagePickerControllerEditedImage] as? UIImage
        profileImage.image = chosenImage

        let user: PFUser = PFUser.currentUser()!
        let profileImageData = UIImageJPEGRepresentation(profileImage.image!, 1)
        if (profileImageData != nil)
        {
            let profileFileObject = PFFile(data: profileImageData!)
            user.setObject(profileFileObject!, forKey: "profilePicture")
            }

        let chosenCoverImage = info[UIImagePickerControllerEditedImage] as? UIImage
        coverImage.image = chosenCoverImage

        let coverUser: PFUser = PFUser.currentUser()!
        let coverImageData = UIImageJPEGRepresentation(coverImage.image!, 1)
        if (coverImageData != nil)
        {
            let coverFileObject = PFFile(data: coverImageData!)
            coverUser.setObject(coverFileObject!, forKey: "coverPicture")
            }

        activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
        activityIndicator.center = self.profileImage.center
        activityIndicator.hidesWhenStopped = true
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.White
        view.addSubview(activityIndicator)
        activityIndicator.startAnimating()
        activityIndicator.color = UIColor.grayColor()

        user.saveInBackgroundWithBlock { (success:Bool, error:NSError?) -> Void in
            self.activityIndicator.stopAnimating()
                    }


        dismissViewControllerAnimated(true, completion: nil)
    }

And here is how I get them from Parse in my ViewDidLoad :

if (PFUser.currentUser()?.objectForKey("profilePicture") != nil)
        {
            let userImageFile:PFFile = PFUser.currentUser()?.objectForKey("profilePicture") as! PFFile
            userImageFile.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
                self.profileImage.image = UIImage(data: imageData!)
            })
        }

        if (PFUser.currentUser()?.objectForKey("coverPicture") != nil)
        {
            let userCoverFile:PFFile = PFUser.currentUser()?.objectForKey("coverPicture") as! PFFile
            userCoverFile.getDataInBackgroundWithBlock({ (imageData: NSData?, error: NSError?) -> Void in
                self.coverImage.image = UIImage(data: imageData!)
            })
        }

Solution

  • this isn't going to be complete, but will have the key parts you will need

    class ResponsePhotoController: UIViewController, UIImagePickerControllerDelegate
    {
        var imageType : Int = 0  // use this to track the image type
                                 // 0 for profile
                                 // 1 for cover
    

    in your code to go get the profile image

    imageType = 0
    

    and then when you go get the cover image

    imageType = 1
    

    and then in the imagePickerController

    if imageType == 0
    {
        // set up the profile picture
    }
    else
    {
        // set up the cover image
    }