0

The PFImageView, with its .file property and its corresponding -loadInBackground method, is really useful. I'm having trouble figuring out how to "go the other way", i.e. get the PFFile reference from a new image:

RA_MyAccount.h (extract)

@property (weak, nonatomic) IBOutlet PFImageView *profilePic;
-(IBAction)saveButtonTapped:(id)sender;

RA_MyAccount.m (extract)

-(void)setLocalImageToPFImageView:(UIImage *)localImage
{
    self.profilePic.image = localImage
}

-(IBAction)saveButtonTapped:(id)sender
{
    PFUser *cUser = [PFUser currentUser];

    // Get the PFFile reference for the new image
    PFFile *file;
    file = ??? ??? ??? ???

    // Set
    cUser[PF_USER_PIC] = file;

    [cUser saveInBackgroundWithBlock:
     ^(BOOL succeeded, NSError *error){
         [self performSegueWithIdentifier:@"unwindtotabviewsegue" sender:self];
     }];
}

I've indicated with ??? ??? ??? ??? where I'm having problems.

max_jf5
  • 169
  • 1
  • 12

2 Answers2

0

So, I have one answer, but I'm just not convinced it's the smartest way. I'm also apprehensive to start using functions like UIImageJPEGRepresentation (is it even a function?) when I don't know anything about image encoding.

file = [PFFile fileWithData:UIImageJPEGRepresentation(self.profilePic.image, 0.8f)];
max_jf5
  • 169
  • 1
  • 12
0

As long as the you're okay with the image losing quality (should be fine if it's a photograph), then you want to do it the following way. The 0.8 represents an 80% compression quality.

file = [PFFile fileWithData:UIImageJPEGRepresentation(self.profilePic.image, 0.8f)];

If you can't lose quality, then do it the following way.

file = [PFFile fileWithData:UIImagePNGRepresentation(self.profilePic.image)];

See this question for a more detailed comparison of the various formats.

Community
  • 1
  • 1
Dehli
  • 5,950
  • 5
  • 29
  • 44