我已經創建了一個應用程序,以允許用戶上傳附帶說明的圖像。兩者都顯示在滾動視圖中。我在圖片下方還有一個UILabel,它應該顯示上傳用戶的詳細信息以及上傳日期。將PFObject分配給UILabel - 將PFUser轉換爲NSString值?
然而,此刻的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"];
而下面是我在哪裏分配一個UILabel 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];
術語KEY_USER
被定義爲@"user"
這讓我困惑的是,我的其他標籤的東西(圖片說明 - 可以隱約看到上傳的圖片左上角「測試3」)訪問descriptionFieldText.text顯示的內容正是用戶輸入的內容?即使當我檢查NSLog
與PFObject objectForKey
相關聯,它顯示一個類似的格式與不受歡迎的UILabel
,但實際的標籤訪問所需的值?正在使用
正在顯示上傳的圖像視圖控制器
修訂 主要方法 - 其中所述的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);
}];
}
爲什麼不使用'[imageObject的setObject:[PFUser currentUser ] .username forKey:@「user」];''或'[imageObject setObject:[PFUser currentUser] .username forKey:@「username」];'instead? – nathan
當我嘗試分配'NSString * userNameLabel = [reportObject setObject:[PFUser currentUser] .username forKey:@「user」];' 我得到錯誤'初始化'NSString * _strong'與不兼容類型的表達' void'' 可能是由於PFObject來自 - (void)動作中的另一個.m文件的事實? 我更新了正在使用的兩個主要方法的主要問題 – FarisZ