gpt4 book ai didi

ios - 将 PFObject 分配给 UILabel - 将 PFUser 转换为 NSString 值?

转载 作者:行者123 更新时间:2023-11-29 00:13:02 25 4
gpt4 key购买 nike

我制作了一个应用程序,以允许用户上传带有附加说明的图像。然后两者都显示在 ScrollView 中。我在图片下方还有一个 UILabel,它应该显示上传用户的详细信息以及上传日期。

但是,目前应该显示用户信息和日期的 UILabel 并没有以我想要的格式(例如“上传者:用户名,日期”)进行显示,而是我获得了以下内容..

UILabel

下面是用户上传图片时使用的PFObject的代码;

PFObject *imageObject = [PFObject objectWithClassName:@"ReportImageObject"];
[imageObject setObject:file forKey:@"image"];
[imageObject setObject:[PFUser currentUser] forKey:@"user"];
[imageObject setObject:self.descriptionTextField.text forKey:@"comment"];

下面是我将 PFObject 的值分配给 UILabel 的地方;

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];

术语KEY_USER 定义为@"user"

让我感到困惑的是,我访问 descriptionFieldText.text 的其他标签(图像描述 - 可以在左上角“测试 3”上传的图片上模糊地看到)显示用户输入的内容?即使我在 NSLog 中检查与它关联的 PFObject objectForKey 并且它显示与不需要的 UILabel 相似的格式,但实际的标签访问期望值?

已更新使用的主要方法

显示上传图像的 View Controller - 显示 UILabel 的地方

-(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也是创建 imageObject 的地方

- (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);
}];
}

最佳答案

只需使用用户名而不是整个用户对象

PFUser *user = (PFUser*)[reportObject objectForKey:KEY_USER];

UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 210, reportImageView.frame.size.width, 15)];
infoLabel.text = [NSString stringWithFormat:@"Uploaded by: %@, %@", user.username, [df stringFromDate:creationDate]];
infoLabel.font = [UIFont fontWithName:@"Arial-ItalicMT" size:9];
infoLabel.textColor = [UIColor blueColor];
infoLabel.backgroundColor = [UIColor clearColor];
[reportImageView addSubview:infoLabel];


PFObject *imageObject = [PFObject objectWithClassName:@"ReportImageObject"];
imageObject[@"image"] = file;
imageObject[@"user"] = [PFUser currentUser];
imageObject[@"comment"] = self.descriptionTextField.text;

关于ios - 将 PFObject 分配给 UILabel - 将 PFUser 转换为 NSString 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45873442/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com