我一直在試圖通過研究和犁過幾天的文檔來弄明白這一點,我無法得到它/我不明白它。Parse.com - 編寫關係查詢?
這裏就是我想要實現:
我有一個名爲「ProfilePhotos」自定義類和一個標準的用戶級。 當我將圖像轉儲到名爲「profilePicture」的列中的主User類時,我有一些「正在工作」的代碼。我需要從ProfilePhotos類的'imageFile'中獲取圖像。
我可以弄清楚如何創建一個類(ProfilePhotos)的圖像,但無法弄清楚如何從圖像所屬的用戶(「名稱」字段)收集數據時調用它們。
我有工作,但有一些莫名其妙的錯誤(圖像加載順序錯誤,一點都沒有,死機)的代碼如下:
@interface CollectionViewController() <UITextViewDelegate>
@property (nonatomic, strong) NSMutableArray *imageArray;
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) PFObject *theName;
@end
@implementation CollectionViewController
- (instancetype) init { //INIT STUFF }
-(void) viewDidLoad { //VIEWDIDLOAD STUFF }
-(void) DataCalls {
PFQuery *getPhotos = [PFUser query];
[getPhotos findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
NSLog(@"Objects Are:%@", objects);
_imageArray = [[NSMutableArray alloc] initWithArray:objects];
for (PFObject* photo in objects ) {
PFFile *pictureFile = [photo objectForKey:@"profilePicture"];
_theName = [photo objectForKey:@"Name"];
[self.collectionView reloadData];
NSLog(@"Name is:%@", _theName);
[pictureFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
NSLog(@"Fetching image..");
[_imageArray addObject:[UIImage imageWithData:data]];
NSLog(@"Size of the _imageArray : %lu", (unsigned long)[_imageArray count]);
} else {
// Log details of the failure
NSLog(@"Error: %@ ", error);
}
}];
}
}
}];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
PhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"photo" forIndexPath:indexPath];
_imageView = (UIImageView *)[cell viewWithTag:200];
PFObject *imageObject = [_imageArray objectAtIndex:indexPath.row];
PFFile *imageFile = [imageObject objectForKey:@"profilePicture"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
_imageView.image = [UIImage imageWithData:data];
}
}];
PFObject *nameText = [imageObject objectForKey:@"Name"];
NSLog(@"Name in the CV:%@", nameText);
cell.userName.text= imageObject[@"Name"];
return cell;
}
您必須爲配置文件映像需要一個分隔類嗎?你想存儲以前的個人資料圖片? – rihe
我確實需要兩個類,因爲我需要知道圖像的createdAt日期並創建了用戶的數據(單獨)。另外,不把所有東西都放在一個巨人班上是不是更好的做法? – Aubrey
在你的User類中製作怪物並不是一個好習慣,但我認爲在這種情況下,從那裏存儲和檢索配置文件圖像要容易得多。通常您會顯示用戶的最近配置文件映像,因此不需要createdAt。但是,如果要存儲用戶的以前的個人資料圖像,請使用不同的課程,但是如果您只需要最新的圖像,則她將其作爲個人資料圖像上傳到用戶級別。 – rihe