如何从当前用户获取所有喜欢的图像(指针)?

How to fetch all liked images (pointer) from current user?

我有一个 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];
    }
}];

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

从 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);
    }
}];

保存所述图像

我会使用图像缓存 Cocoa Pod [ 即。 SAM CACHE].

当我使用 SAM 缓存保存图像文件时,就像这样简单:

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

其中:imageKey 是指附加到数据中对象的唯一 NSString table [ 即。 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 中引用的缓存功能。

更多信息请参考以下link:

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