2015-10-18 48 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

相關問題