[英]Downloading images from Parse iOS
我正在編寫一個允許用戶在Parse上獲取和存儲圖像的應用程序。 到目前為止,我已經設法通過使用以下邏輯將圖像數組保存到Parse:
這就是代碼的樣子; 請原諒,它現在在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];
}];
}
現在,我的問題是:我究竟如何檢索該文件? 我的最終目標是允許用戶查看他們過去拍攝的圖像並添加到集合中的圖片列表並將其上傳到服務器。 我只是不確定如何在上傳文件后檢索文件,並確保保持完整性。
你試過了嗎:
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文件中,您需要具有類似下面的內容。 請注意,我構建的那個可以像一個圖像,然后使用段控制器對喜歡的圖像進行排序。
#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
希望這對你有所幫助。
以上所有解決方案都是正確的,但是想要添加另一種方法來支持SDWebImage的圖像緩存或任何類似的庫。 成功完成后,您將擁有PFFile
,其屬性“url”將返回保存它的Image的實際URL。 您可以使用它來加載圖像。 使用這種方法,我能夠將基於密鑰的圖像緩存作為URL。
...
NSString *strUrl = pfFileObject.url;
...
...
[img sd_setImageWithURL:[NSURL URLWithString:strUrl]];
你為什么要從解析中下載用戶已在本地擁有它們的照片..?
我建議你使用: https : //github.com/AFNetworking/AFNetworking
您還可以將本地照片保存到緩存中,以便輕松訪問它們,這樣您就不需要從解析中下載任何內容了...
現在,如果您仍然想從解析下載照片,只需進行正常查詢並下載所有照片解析對象,您就會在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);
}
}
}];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.