我目前正在开发一款应用程序,即家庭视图控制器显示所有用户朋友,他们的名字和他们的个人资料图片(如果他们有一个)的显示,每个都在UICollectionView的单元格内。我使用标签来识别每个单元格中的UI元素,并使用Parse作为后端。每个用户及其关系都存储在班级"用户"然后他们的个人资料图片(如果他们选择了一个/一个)存储在另一个名为" UserImage"的类中。我设法将他们的用户名设置为单元格但是图像有困难。基本上每个单元格都在下载整个图像阵列,我认为这会导致应用程序因此错误而崩溃。此外,一旦用户添加没有个人资料图片的朋友,该应用程序似乎行为奇怪..
[__NSArrayM objectAtIndex:]: index 8 beyond bounds [0 .. 1]
这是我如何保存图像以解析..这是在用户配置文件视图控制器中。
-(void)uploadImage {
NSData *fileData;
NSString *fileName;
NSString *fileType;
UIImage *newImage = [self scaleImage:self.image toSize:CGSizeMake(340.0,340.0)];
fileData = UIImagePNGRepresentation(newImage);
fileName = @"profileimage.png";
fileType = @"profileimage";
PFFile *file = [PFFile fileWithName:fileName data:fileData];
[file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
PFUser *user = [PFUser currentUser];
PFObject *userImage = [PFObject objectWithClassName:@"UserImage"];
[userImage setObject:file forKey:@"file"];
[userImage setObject:fileType forKey:@"fileType"];
[userImage setObject:user forKey:@"user"];
[userImage saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (error) {
NSLog(@"Error saving image");
}
else {
NSLog(@"Image Saved");
}
}];
}];
}
然后我在HomeViewController的 viewWillAppear:方法中查询当前用户关系。
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self.navigationController.navigationBar setHidden:NO];
self.friendsRelation = [[PFUser currentUser] objectForKey:@"friendsRelation"];
self.friends = [[NSMutableArray alloc] init];
PFQuery *query = [self.friendsRelation query];
[query orderByAscending:@"username"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (error) {
NSLog(@"Error %@ %@", error, [error userInfo]);
}
else {
[self.friends addObjectsFromArray:objects];
[self.collectionView reloadData];
}
}];
}
将单元格数量设置为用户朋友的数量..
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return [self.friends count];
}
最后在 cellForItemAtIndexPath:方法中设置单元格
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
PFUser *user = [self.friends objectAtIndex:indexPath.row];
if (cell.tag == 0) {
UILabel *cellLabel = (UILabel *)[cell viewWithTag:25];
cellLabel.text = user.username;
PFImageView *homeImageView = (PFImageView *)[cell viewWithTag:50];
[homeImageView setContentMode:UIViewContentModeScaleAspectFill];
homeImageView.clipsToBounds = YES;
homeImageView.layer.cornerRadius = 7;
self.friendImages = [[NSMutableArray alloc] init];
PFQuery *imageQuery = [PFQuery queryWithClassName:@"UserImage"];
[imageQuery whereKey:@"user" containedIn:self.friends];
[imageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
//need a statement that defines whether a friend has an image or not, if they do then load that image into their cell, if not then set default image
[self.friendImages addObjectsFromArray:objects];
PFObject *imageObject = [self.friendImages objectAtIndex:indexPath.row];
PFFile *imageFile = [imageObject objectForKey:@"file"];
homeImageView.file = imageFile;
[homeImageView loadInBackground];
NSLog(@"Retrieved %d images", [self.friendImages count]);
}
else {
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
return cell;
}
任何帮助将不胜感激!请记住,我只需要几周的时间来学习iOS ..