在UICollectionView中显示下载的图像

时间:2014-05-10 00:36:18

标签: ios objective-c uicollectionview uicollectionviewcell

我目前正在开发一个应用程序,它允许用户拍摄图像并以阵列的形式将它们存储到Parse上的唯一对象(允许多个图像等)。我想要发生的是下次用户访问该对象时,他们之前保存的所有图像都将出现在UICollectionView中。我环顾四周并遵循教程,但是我遇到了奇怪的错误并且无法显示图像。这是我到目前为止所有相关的代码:

- 下载要显示的图像的视图:

查询解析

- (void)queryParseMethod
{
PFQuery *tankQuery = [PFQuery queryWithClassName:@"SavedTanks"];
[tankQuery whereKey:@"objectId" equalTo:_passedValue];

[tankQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
    if (!error)
    {
        for (PFObject *object in objects)
        {
            PFFile *imageFile = [object valueForKey:@"tankImages"];
            [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
                if (!error)
                {
                    imagesFilesArray = [[NSArray alloc] initWithObjects:imageFile, nil];
                    NSLog(@"NO ERROR HERE: %@", imagesFilesArray);
                }
                else
                {
                    NSLog(@"THERE WAS AN ERROR: %@", error);
                }
                [_imagesCollection reloadData];
            }];
        }
    }
}];
}

注销时,此功能可靠地抓取正确的PFFile。

UICollectionView CellForItemAtIndexPath更新

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

static NSString *cellIdentifier = @"parseImage";
    [collectionView registerClass:[photoHandler class] forCellWithReuseIdentifier:cellIdentifier];
photoHandler *cell = (photoHandler *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

PFFile *imageObject = [imagesFilesArray objectAtIndex:indexPath.row];


[imageObject getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
    if (!error) {
        cell.parseImage.image = [UIImage imageWithData:data];
    }
}];
return cell;
}

parseImage 是一个UIImageView,它隐藏在 UICollectionViewCell 类中。

我目前收到的错误是: [PFFile objectForKey:]:无法识别的选择器发送到实例

这里是处理collectionView的绘图的其余代码:

- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}

- (NSInteger) collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
NSLog(@"Number of items in section: %lu", (unsigned long) imagesFilesArray.count);
return [imagesFilesArray count];
}

- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

}

尽我所能,我似乎无法弄清楚出了什么问题。界面构建器中的所有项目都连接到正确的连接。谁能帮我?这是我在最后一轮QA然后发布之前想要开始工作的最后一个大功能:)非常兴奋,因为这是我的第一个个人应用程序但是你可以想象的很沮丧。

再次编辑:photoHandler文件

·H

@interface photoHandler : UICollectionViewCell

{

}

@property (weak, nonatomic) IBOutlet UIImageView *parseImage;

@end

的.m

@interface photoHandler ()

@end

@implementation photoHandler

@synthesize parseImage;

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
    // init code
}
return self;
}

双重编辑 - 上传代码

- (void) imagePickerController:(UIImagePickerController *)picker      didFinishPickingMediaWithInfo:(NSDictionary *)info
{
_takenImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:YES completion:^
{
// Add object to array: Working
[_tankImagesArray addObject:_takenImage];
NSLog(@"Number of images taken: %lu", (unsigned long)_tankImagesArray.count);

// Convert array to NSData Object
NSData *imageData = [NSKeyedArchiver archivedDataWithRootObject:_tankImagesArray];

// Convert NSData Object to PFFile
PFFile *imageFile = [PFFile fileWithData:imageData];

PFQuery *tankQuery = [PFQuery queryWithClassName:@"SavedTanks"];
_tankObject = [tankQuery getObjectWithId:_passedValue];

[_tankObject setObject:imageFile forKey:@"tankImages"];

[_tankObject save];
}];
}

0 个答案:

没有答案