1

私は数日間、調査とドキュメントを調べてこれを理解しようとしてきましたが、理解できません/理解できません。

これが私が達成しようとしていることです:

ここに画像の説明を入力

「ProfilePhotos」という名前のカスタム クラスと標準の User クラスがあります。「profilePicture」という列のメイン User クラスに画像をダンプすると、「機能する」コードがいくつかあります。ProfilePhotos クラスの「imageFile」から画像を取得する必要があります。

画像を 1 つのクラス (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;


}
4

2 に答える 2

1

usernameポインターを使用し続けます。物事をリンクするためにポインターを使用することに切り替えないでください。includeKey:クエリで使用して、完全に入力されたポインターを返すことを忘れないでください。

PFQuery *query = [PFQuery queryWithClassName:@"ProfilePhotos"]

// apply any filters, here's how to filter by user:
// could just as easily be another user
PFUser *user = [PFUser currentUser];
[query whereKey:@"user" equalTo:user];

// this allows reading of user properties in the results
[query includeKey:@"user"];

[query orderByDescending:@"createdAt"];
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
    if (error) {
        NSLog(@"Error.");
    } else {
        PFFile *file = object[@"imageFile"];
        PFUser *pictureUser = object[@"user"];
        NSString *name = pictureUser[@"Name"];
    }
}];
于 2014-06-12T19:34:23.173 に答える