I've made an app in order to allow the user to upload an image with a description attached to it. Both are then displayed in a scrollview. I also have a UILabel below the image which should display the details of the user who uploaded it, and the date it was uploaded.
However, at the moment the UILabel which should display user information and date doesn't do it in the format I would desire (e.g. "Uploaded by: username, date") instead I obtain the following..
Below is the code for the PFObject which is used when the user uploads a picture;
PFObject *imageObject = [PFObject objectWithClassName:@"ReportImageObject"];
[imageObject setObject:file forKey:@"image"];
[imageObject setObject:[PFUser currentUser] forKey:@"user"];
[imageObject setObject:self.descriptionTextField.text forKey:@"comment"];
And below is where I am assigning a UILabel the values of the PFObject;
NSDate *creationDate = reportObject.createdAt;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm dd/MM yyyy"];
UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 210, reportImageView.frame.size.width, 15)];
infoLabel.text = [NSString stringWithFormat:@"Uploaded by: %@, %@", [reportObject objectForKey:KEY_USER], [df stringFromDate:creationDate]];
infoLabel.font = [UIFont fontWithName:@"Arial-ItalicMT" size:9];
infoLabel.textColor = [UIColor blueColor];
infoLabel.backgroundColor = [UIColor clearColor];
[reportImageView addSubview:infoLabel];
The term KEY_USER is defined as @"user"
The thing which confuses me is that my other label (the image description - can be vaguely seen on the uploaded picture on the top left "Test 3") which accesses the descriptionFieldText.text displayed exactly what the user enters? Even when I check the NSLog for the PFObject objectForKey associated with that and it shows a similar format to the undesired UILabel, but the actual Label accesses the desired value?
UPDATED Main methods being used
The View controller where the uploaded images are being displayed - where the UILabel is being shown
-(void)getReportImages{
//Fetch images
PFQuery *query = [PFQuery queryWithClassName:@"ReportImageObject"];
[query orderByDescending:KEY_CREATION_DATE];
[query findObjectsInBackgroundWithBlock:^(NSArray * _Nullable objects, NSError * _Nullable error) {
if (!error) {
self.reportsObjectArray = nil;
self.reportsObjectArray = [[NSArray alloc] initWithArray:objects];
[self loadReportViews];
} else {
[self showErrorView:error];
}
}];
}
-(void)loadReportViews{
for (id viewToRemove in [self.reportsScroll subviews]) {
if ([viewToRemove isMemberOfClass:[UIView class]])
[viewToRemove removeFromSuperview];
}
int originY = 10;
for (PFObject *reportObject in self.reportsObjectArray){
UIView *reportImageView = [[UIView alloc] initWithFrame:CGRectMake(10, originY, self.view.frame.size.width -20, 300)];
//Adds image
PFFile *image = (PFFile*)[reportObject objectForKey:KEY_IMAGE];
UIImageView *userImage = [[UIImageView alloc] initWithImage:[UIImage imageWithData:image.getData]];
userImage.frame = CGRectMake(0, 0, reportImageView.frame.size.width, 200);
[reportImageView addSubview:userImage];
//Adds image info
NSDate *creationDate = reportObject.createdAt;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm dd/MM yyyy"];
UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 210, reportImageView.frame.size.width, 15)];
infoLabel.text = [NSString stringWithFormat:@"Uploaded by: %@, %@", [reportObject objectForKey:KEY_USER], [df stringFromDate:creationDate]];
infoLabel.font = [UIFont fontWithName:@"Arial-ItalicMT" size:9];
infoLabel.textColor = [UIColor blueColor];
infoLabel.backgroundColor = [UIColor clearColor];
[reportImageView addSubview:infoLabel];
//Adds image description
UILabel *descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 24, reportImageView.frame.size.width, 15)];
descriptionLabel.text = [reportObject objectForKey:KEY_COMMENT];
descriptionLabel.font = [UIFont fontWithName:@"ArialMT" size:13];
descriptionLabel.textColor = [UIColor whiteColor];
descriptionLabel.backgroundColor = [UIColor clearColor];
[reportImageView addSubview:descriptionLabel];
[self.reportsScroll addSubview:reportImageView];
originY = originY + reportImageView.frame.size.width + 20;
NSLog(@"Test of description%@",descriptionLabel);
NSLog(@"The content of infolabel %@", infoLabel);
}
self.reportsScroll.contentSize = CGSizeMake(self.reportsScroll.frame.size.width, originY);
}
UIViewController where the user assigns the image and the image description Also where imageObject is being created
- (void)sendPressed:(id)sender{
[self.descriptionTextField resignFirstResponder];
self.navigationItem.rightBarButtonItem.enabled = NO;
UIActivityIndicatorView *loadingSpinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[loadingSpinner setCenter:CGPointMake(self.view.frame.size.width/2.0, self.view.frame.size.height/2.0)];
[loadingSpinner startAnimating];
[self.view addSubview:loadingSpinner];
//Upload a new picture if not currently uploaded.
NSData *pictureData = UIImagePNGRepresentation(self.imageToUpload.image);
PFFile *file = [PFFile fileWithName:@"image" data:pictureData];
[file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
PFObject *imageObject = [PFObject objectWithClassName:@"ReportImageObject"];
[imageObject setObject:file forKey:@"image"];
[imageObject setObject:[PFUser currentUser] forKey:@"user"];
[imageObject setObject:self.descriptionTextField.text forKey:@"comment"];
[imageObject saveInBackgroundWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
if (succeeded) {
[self.navigationController popViewControllerAnimated:YES];
}
else {
NSString *errorString = [[error userInfo] objectForKey:@"error"];
UIAlertController *errorAlertView = [UIAlertController alertControllerWithTitle:@"Error" message:errorString preferredStyle:UIAlertControllerStyleAlert];
[errorAlertView addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:errorAlertView animated:YES completion:nil];
}
}];
}
else{
NSString *errorString = [[error userInfo] objectForKey:@"error"];
UIAlertController *errorAlertView = [UIAlertController alertControllerWithTitle:@"Error" message:errorString preferredStyle:UIAlertControllerStyleAlert];
[errorAlertView addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:nil]];
[self presentViewController:errorAlertView animated:YES completion:nil];
}
}progressBlock:^(int percentDone) {
NSLog(@"Uploaded: %d %%", percentDone);
}];
}

Just use the username instead of the whole user object