私は数日間、調査とドキュメントを調べてこれを理解しようとしてきましたが、理解できません/理解できません。
これが私が達成しようとしていることです:
「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;
}