0

I can view pictures from Parse on UIImage but I can't view a .txt file stored onto Parse on a UIImage. I know if the document is larger than 10MB you must save it on the cloud. This .txt file is small. Please help.

    var query = PFQuery(className:"_User")
    query.findObjectsInBackgroundWithBlock({ (objects: [AnyObject]?, error: NSError?) -> Void in
  if let objects = objects 
  {
     for object in objects 
     {
        if let resultView = PFUser.currentUser()?["file"] as? PFFile 
        {
           //Thats NOT unwrapped ? Need to use !
        resultView.getDataInBackgroundWithBlock { (data: NSData?, error: NSError?) -> Void in
             if (error == nil) 
             {
                  let image = UIImage(data: data!)
                 self.imageView.image = UIImage(data: data!)
                println(data)
              }
         }

       }
    }

    })
Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70

3 Answers3

0

You can't populate a UIImage with a text file.

You are going to end up doing some useless hackery in order to circumvent simply storing a String and using a UILabel.

I suggest storing a String and populating a UILabel or a UITextView

But then again, it depends on your use case. What is your use case?

ray
  • 102
  • 1
  • 8
0

This line

   if let resultView = PFUser.currentUser()?["file"] as? PFFile 

should be replace by

let resultView = object["file"] as? PFFile 

And also I don't think you could display .txt file into a UIImageView

Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
Lamour
  • 3,002
  • 2
  • 16
  • 28
0

First of all, PFUser.currentUser() is referring to the logged-in user. Since, you are querying the _User table. So, objects are an array of PFUser. What's the reason why you need to loop through all the objects?

It's okay to use PFFile for storing text file. Basically, you convert NSString to NSData and then store it into PFFile. When you read it, you should use the similar method to convert back to NSString. Here we go: NSString* textString = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];

I think the wrong concept you have is: you think of a .txt can be handled directly by PFFile. You should find a way to load .txt into NSString. Then, you have the control.

Lucas Huang
  • 3,998
  • 3
  • 20
  • 29