2015-10-18 99 views
1

我有一个解析类“活动”和“照片”。如何从当前用户获取PFObjects喜欢的图像的NSArray? Activity class如何从当前用户获取所有喜欢的图片(指针)?

Photo class

PFQuery *queryLikedPhoto = [PFQuery queryWithClassName:@"Activity"]; 
[queryLikedPhoto whereKey:@"type" equalTo:@"like"]; 
[queryLikedPhoto whereKey:@"fromUser" equalTo:[PFUser currentUser]]; 
[queryLikedPhoto includeKey:@"photo"]; 
[queryLikedPhoto findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    if (!error) { 
     for (PFObject *object in objects) { 
      PFObject *photoObject = [object objectForKey:@"photo"]; 
      [photoObject fetchIfNeededInBackgroundWithBlock:^(PFObject *photoObjects, NSError *error) { 
       NSLog(@"photoObjects: %@", photoObjects); 
      }]; 
     } 
    } 
}]; 

它检索与图像PFOjects,但如何将其保存到NSArray中进一步使用? (与NSMutableArray的变种不工作):

PhotoObjects: <Photo: 0x166baf30, objectId: Q0H7XKYeCU, localId: (null)> { image = "<PFFile: 0x166ba810>"; thumbnail = "<PFFile: 0x166ba450>"; user = "<PFUser: 0x1668c6f0, objectId: qGYdDnAtbD, localId: (null)>"; username = "Tim"; } 2015-10-18 23:39:57.058 MyApp[5817:572564] photoObjects: <Photo: 0x166bdec0, objectId: eWGonc9YLz, localId: (null)> { image = "<PFFile: 0x166bd9e0>"; thumbnail = "<PFFile: 0x166bd6a0>"; user = "<PFUser: 0x1668c6f0, objectId: qGYdDnAtbD, localId: (null)>"; username = "Steve"; }

这个查询看起来更好 编辑:

PFQuery *queryLikedPhoto = [PFQuery queryWithClassName:@"Activity"]; 
[queryLikedPhoto whereKey:@"type" equalTo:@"like"]; 
[queryLikedPhoto whereKey:@"fromUser" equalTo:[PFUser currentUser]]; 
[queryLikedPhoto whereKeyExists:@"photo"]; 
[queryLikedPhoto findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    if (!error) { 
     self.likedPhotoObjects = [NSMutableArray array]; 
     if (objects.count >0) { 
      for (PFObject *object in objects) { 
       PFObject *photoObject = [object objectForKey:@"photo"]; 
       [self.likedPhotoObjects addObject:photoObject]; 
      } 
     } 
      [self.collectionView reloadData]; 
    } 
}]; 
+0

@ lightice11阅读时,我一直在努力,检索和写入的NSMutableArray但总是空,没有任何解决方案如何保存以供进一步使用?! – vbondarenko3d

回答

0

提取图像从PFQUERY

你似乎是治疗你的 “照片” PFFile对象作为PFObject而不是PFFile。

将下面的代码查询块中:

PFFile *imageFile = [object objectForKey:@"photo"]; 

[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) 
{ 
    if (!error) 
    { 
     // SUCCESS. 

     imageView.image = [UIImage imageWithData:data];      
    } 
    else 
    { 
     // ERROR. 

     NSLog(@"%@", error); 
    } 
}]; 

SAVE所述图像

我会用图像缓存可可豆荚[即。 SAM CACHE]。

当我使用SAM缓存保存的图像文件很简单,只要:

[[SAMCache sharedCache] setImage:[UIImage imageWithData:data] forKey:imageKey]; 

,其中:imageKey参照数据表内[即附接至对象的唯一的NSString。 ObjectId]。


检索所述图像

要检索在今后的图像文件,然后我执行下面的代码行:

UIImage *cacheImage = [[SAMCache sharedCache] imageForKey:imageKey]; 

检查是否图像是CACHE

要检查图像是否存在缓存中的imageKey

UIImage *cacheImage = [[SAMCache sharedCache] imageForKey:imageKey]; 

if (cacheImage) 
{ 
    // Image exists within cache. 
} 
else 
{ 
    // Image does not exist within cache. 
} 

替代品CACHING

Parse.com还提供了SDK中引用的缓存功能。

请参考以下链接了解详细信息:

https://www.parse.com/docs/ios/guide#queries-caching-queries


+1

感谢您的帮助! – vbondarenko3d

0

从您在粘贴打印出来看,查询工作正常。看看如何处理PFFiles的指南。到目前为止,您所做的只是获取指向文件的指针,所以现在您需要下载文件本身。 (It's all in the guide

相关问题