抓取解析iOS中每个对象的第一个图像
很抱歉标题措词不当。我有一个逻辑问题,我正试着去想。我正在使用的视图有一个抓取解析iOS中每个对象的第一个图像,ios,objective-c,uicollectionview,parse-platform,Ios,Objective C,Uicollectionview,Parse Platform,很抱歉标题措词不当。我有一个逻辑问题,我正试着去想。我正在使用的视图有一个UICollectionView,它显示与用户关联的“坦克”列表。此集合视图显示三个项目: 坦克名称 油罐容量 上次存储的图像 最后一个图像存储部分是我遇到问题的地方。我正在取得进展,但这是一个逻辑问题背后,我不确定。以下是数据的外观: 我有两个班级,我正在与之互动保存的银行和保存的银行图像。保存的储罐中唯一的objectId也作为值存储在SavedTankImages中,以允许对图像进行某种指针引用。当用户加载一个“
UICollectionView
,它显示与用户关联的“坦克”列表。此集合视图显示三个项目:
- 坦克名称
- 油罐容量
- 上次存储的图像
保存的银行图像
。保存的储罐中唯一的objectId
也作为值存储在SavedTankImages
中,以允许对图像进行某种指针引用。当用户加载一个“坦克”并且可以看到他们存储的与之相关的所有图像时,这个逻辑就起作用了
然而,为了这个视图的目的,我只需要从每个坦克抓取第一个图像并显示它。这就是我需要帮助的地方。以下是我目前掌握的情况:
#pragma mark COLLECTION VIEW
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
photoHandler *cell = (photoHandler *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
_tankNameArray = [_array objectAtIndex:indexPath.section * 1 + indexPath.row];
cell.tankNameLabel.text = [_tankNameArray valueForKey:@"tankName"];
cell.tankCapLabel.text = [_tankNameArray valueForKey:@"tankCapacity"];
NSArray *objectId = [_array valueForKey:@"objectId"];
for (int i = 0; i < objectId.count; i++)
{
NSString *objectString = [[NSString alloc] init];
objectString = [objectId objectAtIndex:i];
PFQuery *imageQuery = [PFQuery queryWithClassName:@"SavedTankImages"];
[imageQuery whereKey:@"tankObjectId" equalTo:objectString];
[imageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error)
{
for (PFObject *object in objects)
{
NSLog(@"OBJECT TEST: %@", object);
}
}
}];
}
return cell;
}
SavedTankImages:
是单个图像的objectId
,而tankObjectId
是图像关联的储罐。我离目标越来越近了,但我需要知道如何有效地进行迭代,并且只获取tankObjectId
与原始objectId
匹配的第一项。如果这听起来有点复杂,请原谅我
像往常一样提前谢谢你的帮助
更新
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
photoHandler *cell = (photoHandler *)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
_tankNameArray = [_array objectAtIndex:indexPath.section * 1 + indexPath.row];
cell.tankNameLabel.text = [_tankNameArray valueForKey:@"tankName"];
cell.tankCapLabel.text = [_tankNameArray valueForKey:@"tankCapacity"];
NSArray *objectId = [_array valueForKey:@"objectId"];
for (int i = 0; i < objectId.count; i++)
{
// NSString *objectString = [[NSString alloc] init];
// objectString = [objectId objectAtIndex:i];
PFQuery *imageQuery = [PFQuery queryWithClassName:@"SavedTankImages"];
[imageQuery whereKey:@"tankObjectId" equalTo:[objectId objectAtIndex:i]];
[imageQuery getFirstObjectInBackgroundWithBlock:^(PFObject *objects, NSError *error)
{
if (!error)
{
PFFile *imageFile = [objects valueForKey:@"tankImages"];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error)
{
cell.parseImage.image = [UIImage imageWithData:data];
}
}];
NSLog(@"Heres your image: %@", objects);
}
}];
}
return cell;
}
-(UICollectionViewCell*)collectionView:(UICollectionView*)collectionView cellForItemAtIndexPath:(NSIndexPath*)indexPath
{
静态NSString*CellIdentifier=@“Cell”;
photoHandler*单元格=(photoHandler*)[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
_tankNameArray=[\u数组objectAtIndex:indexath.section*1+indexPath.row];
cell.tanknamelab.text=[\u tanknamerary valueForKey:@“tankName”];
cell.tankCapLabel.text=[\u tankNameArray valueForKey:@“tankCapacity”];
NSArray*objectId=[[数组值forkey:@“objectId]”;
for(int i=0;i
上面的代码选择第一个可用图像,并使其成为collectionView
中每个单元格的背景。我希望得到它,以便它只返回objectId的第一个图像。换句话说
- 油箱1=油箱1图像1
- 储罐2=储罐2图像1
- 储罐3=储罐3图像1
- 油箱1=油箱1图像1
- 油箱2=油箱1图像1
- 油箱3=油箱1图像1
// sort by createdAt or use updatedAt
[imageQuery orderByDescending:@"createdAt"];
// change from find to getFirst
//[imageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
[imageQuery getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!error)
{
NSLog(@"OBJECT TEST: %@", object);
}
}];
如果您经常对此进行查询,则更高级的解决方案是在水箱上存储指向最新对象的指针。您可以在代码中执行此操作,也可以创建一个云代码方法,以便在保存每个SavedTankImages
对象后自动更新它(它只需加载相关的savedTank
,并将mostrecentImages
设置为指向保存的图像,然后保存SavedTanks
)
如果您已经这样做了,那么您可以使用
include:
方法加载mostRecentImage
和SavedTanks您能澄清这个问题吗?所以你当前的代码是提取所有与坦克相关的图像,你只需要最新的图像,对吗?没错。我只想提取与每个油箱相关的最新数据并显示出来。这里的用例是一个与用户关联的坦克列表,我使用的不是tableView,而是collectionView,这样我可以轻松地显示图像。大部分都在工作。唯一没有的是抓取第一张显示更新代码的图像;它只做了我希望它做的一半,不过我现在离它更近了。
// sort by createdAt or use updatedAt
[imageQuery orderByDescending:@"createdAt"];
// change from find to getFirst
//[imageQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
[imageQuery getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!error)
{
NSLog(@"OBJECT TEST: %@", object);
}
}];