我有一个Parse类“Activity”和“Photo”。如何从currentUser获取PFObjects的NSArray喜欢的图像?
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 :(得分:0)
根据您粘贴的打印件判断,查询工作正常。查看有关如何处理PFFiles的指南。到目前为止你所做的就是获取指向文件的指针,所以现在你需要自己下载文件。 (It's all in the guide)
答案 1 :(得分:0)
从PFQUERY中提取图片
你似乎正在治疗你的&#34;照片&#34; 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);
}
}];
保存图片
我会使用图像缓存Cocoa Pod [即。 SAM CACHE]。
使用SAM缓存时,保存图像文件非常简单:
[[SAMCache sharedCache] setImage:[UIImage imageWithData:data] forKey:imageKey];
其中:imageKey是指附加到数据表中对象的唯一NSString [即。 ObjectId]。
检索出图像
为了将来检索图像文件,我执行以下代码行:
UIImage *cacheImage = [[SAMCache sharedCache] imageForKey:imageKey];
检查图像是否处于缓存状态
检查imageKey缓存中是否存在图像
UIImage *cacheImage = [[SAMCache sharedCache] imageForKey:imageKey];
if ( cacheImage )
{
// Image exists within cache.
}
else
{
// Image does not exist within cache.
}
用于缓存的替代方案
Parse.com还提供了SDK中引用的缓存功能。
有关详细信息,请参阅以下链接:
https://www.parse.com/docs/ios/guide#queries-caching-queries