从Parse iOS下载图像

我正在编写一个应用程序,允许用户在Parse上获取和存储图像。 到目前为止,我已经设法通过使用以下逻辑完成将图像数组保存到分析:

  • 采取形象
  • 将对象添加到数组
  • 将数组转换为NSData
  • 将NSData转换为PFFile
  • 设置file upload目的地(通过唯一的objectId)
  • 上传PFFileparsing

这是代码的样子; 请原谅它现在在dismissViewController的事实,我只是试图让它保存成功:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { _takenImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage]; [self dismissViewControllerAnimated:YES completion:^ { // Add object to array: Working [_tankImagesArray addObject:_takenImage]; NSLog(@"Number of images taken: %lu", (unsigned long)_tankImagesArray.count); // Convert array to NSData Object NSData *imageData = [NSKeyedArchiver archivedDataWithRootObject:_tankImagesArray]; // Convert NSData Object to PFFile PFFile *imageFile = [PFFile fileWithData:imageData]; PFQuery *tankQuery = [PFQuery queryWithClassName:@"SavedTanks"]; _tankObject = [tankQuery getObjectWithId:_passedValue]; [_tankObject setObject:imageFile forKey:@"tankImages"]; [_tankObject save]; }]; } 

现在,我的问题是:我将如何去检索该文件? 我的最终目标是允许用户查看他们过去拍摄的图像,并添加到collections中的图片列表中,并将其上传到服务器。 我只是不确定如何检索文件一旦上传,并确保完整性保持。

你试过了吗:

 PFQuery *query = [PFQuery queryWithClassName:@"SavedTanks"]; [query whereKey:@"tankImages" equalTo:@"your_image.jpg"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (!error) { // The find succeeded. NSLog(@"Successfully retrieved %d images.", objects.count); // Do something with the found objects for (PFObject *object in objects) { NSLog(@"%@", object.objectId); } } else { // Log details of the failure NSLog(@"Error: %@ %@", error, [error userInfo]); } }]; 
 PFQuery *query = [PFQuery queryWithClassName:@"SavedTanks"]; // Add constraints here to get the image you want (like the objectId or something else) [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (!error) { for (PFObject *object in objects) { PFFile *imageFile = object[@"tankImages"]; [imageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) { if (!error) { UIImage *image = [UIImage imageWithData:imageData]; // Here is your image. Put it in a UIImageView or whatever } }]; } } else { // Log details of the failure } }]; 

在你的集合视图的.h文件中,你需要像下面这样的东西。 请注意,我build立你可以喜欢一个图像,然后使用段控制器sorting喜欢的图像。

 #import <UIKit/UIKit.h> #import "UICollectionCell.h" #import <Parse/Parse.h> @interface ParseViewController : UIViewController { NSArray *imageFilesArray; NSMutableArray *imagesArray; } @property (weak, nonatomic) IBOutlet UICollectionView *imagesCollection; - (IBAction)segmentSelected:(id)sender; @property (weak, nonatomic) IBOutlet UISegmentedControl *segmentedController; @end 

然后在集合视图的.m文件中

  @interface ParseViewController () @end @implementation ParseViewController @synthesize imagesCollection; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self queryParseMethod]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } // code to add the number of images etc as per table view -(void) queryParseMethod { NSLog(@"start query"); PFQuery *query = [PFQuery queryWithClassName:@"collectionView"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (!error) { imageFilesArray = [[NSArray alloc] initWithArray:objects]; NSLog(@"%@", imageFilesArray); [imagesCollection reloadData]; } }]; } #pragma mark - UICollectionView data source -(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { // number of sections return 1; } -(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { // number of items return [imageFilesArray count]; } -(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath { // the custom cell we named for the reusable identifier static NSString *cellIdentifier = @"imageCell"; UICollectionCell *cell = (UICollectionCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath]; PFObject *imageObject = [imageFilesArray objectAtIndex:indexPath.row]; PFFile *imageFile = [imageObject objectForKey:@"imageFile"]; // show loading spinner [cell.loadingSpinner startAnimating]; cell.loadingSpinner.hidden = NO; [imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) { if (!error) { NSLog(@"%@", data); cell.parseImage.image = [UIImage imageWithData:data]; [cell.loadingSpinner stopAnimating]; cell.loadingSpinner.hidden = YES; } }]; return cell; } -(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { [self likeImage:[imageFilesArray objectAtIndex:indexPath.row]]; } -(void) likeImage:(PFObject *)object { [object addUniqueObject:[PFUser currentUser].objectId forKey:@"favorites"]; [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (!error) { NSLog(@"liked picture!"); [self likedSuccess]; } else { [self likedFail]; } }]; } -(void) likedSuccess { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"You have succesfully liked the image" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; } -(void) likedFail { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unsuccesfull" message:@"You have been unable to like the image" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil]; [alert show]; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ - (IBAction)segmentSelected:(id)sender { if (_segmentedController.selectedSegmentIndex == 0) { [self queryParseMethod]; } if (_segmentedController.selectedSegmentIndex == 1) { [self retrieveLikedImages]; } } -(void) retrieveLikedImages { PFQuery *getFavorites = [PFQuery queryWithClassName:@"collectionView"]; [getFavorites whereKey:@"favorites" equalTo:[PFUser currentUser].objectId]; [getFavorites findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (!error) { imageFilesArray = [[NSArray alloc] initWithArray:objects]; [imagesCollection reloadData]; } }]; } @end 

希望这对你有一些帮助。

以上所有解决scheme都是正确的,但是想要添加另一种方式来支持SDWebImage的图像caching或任何类似的库。 成功完成后,您将拥有PFFile ,其属性“url”将返回您执行保存图像的URL。 你可以使用它来加载图像。 使用这种方法,我可以使用基于密钥的图像caching作为URL。

 ... NSString *strUrl = pfFileObject.url; ... ... [img sd_setImageWithURL:[NSURL URLWithString:strUrl]]; 

你为什么要从parsing下载的照片,用户已经有他们在本地..?

我build议你使用: https : //github.com/AFNetworking/AFNetworking

你也可以保存本地照片caching,所以你很容易访问它们,所以你不需要从parsing下载…

现在如果你仍然想从parsing下载照片只是作出一个正常的查询,并下载所有的照片parsing对象,你会在PFObject PFFile你的照片。

例:

 PFQuery *query = [PFQuery queryWithClassName:@"SavedTanks"]; [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { if (!error) { for(PFObject *obj in objects){ PFFile *file = [obj objectForKey:@"tankImages"]; // now you can use this url to download the photo with AFNetwork NSLog(@"%@",file.url); } } }];