我试图使用解析从指针获取图像。我试图从一个类中提取图像,而没有再发出第二个请求。
以下是代码:
PFFile * connectImageFile = [object objectForKey:@"prayerImage"];
connection.connectImage.file = connectImageFile;
[connection.connectImage loadInBackground];
第二行引发以下异常:
-[PFObject url]: unrecognized selector sent to instance 0x16d4f480
我确信我正在使用PFTableViewCell(它位于PFTableViewController中),而我connectImage
在代码和Interface Builder中是PFImageViewer
并且它是链接的。我省略了最后两行并添加了:
NSLog(@"%@", connectImageFile.url);
出于调试目的而仍然出现错误:
-[PFObject url]: unrecognized selector sent to instance 0x14557680
显然这是一个不同的实例,但看似相同的问题...当我NSLog只是connectImageFile
然后我得到:<prayers:W9SUTK3uL3:(null)>
这是正确的objectID ...所以我完全不知道。
修改:
为了安全起见,我对图像进行了单独查询,但仍有问题。当我尝试填充PFImageView
时它并没有崩溃,但是当我尝试从PFObject
获取网址时崩溃了。这是我的代码:
PFObject * prayerImageObject = [object objectForKey:@"prayerImage"];
NSLog(@"%@", prayerImageObject.objectId);
PFQuery * query = [PFQuery queryWithClassName:@"prayers"];
[query whereKey:@"ObjectId" equalTo:prayerImageObject.objectId];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
PFFile * connectImageFile = [object objectForKey:@"prayerImage"];
connection.imageView.file = connectImageFile;
NSLog(@"%@", connectImageFile.url);
[connection.imageView loadInBackground];
}];
我的第一个NSLog
给了我一个好objectId
,并且与我想要接收的正确图像一致。我的第二个NSLog
是抛出异常的地方(非常类似于整个PFObject
交易,除了具有不同的十六进制值)。
修改的 根据请求,这是我的实施文件:
//
// connectTableViewController.m
// Prayer
//
// Created by Evan Is Stupid on 12/29/13.
// Copyright (c) 2013 Evan Stoddard. All rights reserved.
//
#import "connectTableViewController.h"
#import "connectTableViewCell.h"
#import <Parse/Parse.h>
@interface connectTableViewController ()
@property (nonatomic, retain) connectTableViewCell * connection;
@end
@implementation connectTableViewController
@synthesize connection;
- (id)initWithCoder:(NSCoder *)aDecoder{
self = [super initWithCoder:aDecoder];
if (self) {
// Custom the table
// The className to query on
self.parseClassName = @"connections";
// Whether the built-in pull-to-refresh is enabled
self.pullToRefreshEnabled = YES;
// Whether the built-in pagination is enabled
self.paginationEnabled = YES;
// The number of objects to show per page
self.objectsPerPage = 5;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.connection = [[connectTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"connectionCell"];
}
- (void)objectsDidLoad:(NSError *)error {
[super objectsDidLoad:error];
}
- (void)objectsWillLoad {
[super objectsWillLoad];
}
- (PFQuery *)queryForTable {
PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
[query whereKey:@"username" equalTo:[PFUser currentUser]];
// If no objects are loaded in memory, we look to the cache first to fill the table
// and then subsequently do a query against the network.
if ([self.objects count] == 0) {
query.cachePolicy = kPFCachePolicyCacheThenNetwork;
}
return query;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {
return [self loadTableViewObjects:object withTableView:tableView];
}
- (connectTableViewCell *)loadTableViewObjects:(PFObject *)object withTableView:(UITableView *)tableView{
connection = [tableView dequeueReusableCellWithIdentifier:@"connectionCell"];
PFObject * prayerImageObject = [object objectForKey:@"prayerImage"];
NSLog(@"%@", prayerImageObject.objectId);
PFQuery * query = [PFQuery queryWithClassName:@"prayers"];
[query whereKey:@"ObjectId" equalTo:prayerImageObject.objectId];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
PFFile * connectImageFile = [object objectForKey:@"prayerImage"];
connection.imageView.file = connectImageFile;
NSLog(@"%@", connectImageFile.url);
[connection.imageView loadInBackground];
}];
connection.connectTitle.text = @"Someone has prayed for you!";
return connection;
}
@end
答案 0 :(得分:1)
如果您试图通过它的指针获取对象内的图像,则需要先调用fetch
。
所以,如果我理解你的问题,你应该尝试:
PFObject *object...
[object fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
if (!error) {
// Now get the file
PFFile * connectImageFile = [object objectForKey:@"prayerImage"];
// continue your code
}
}];
<强>更新强>
这是适合我的代码。不幸的是,我不能只用一个请求,就像你想要的那样。为此,您必须已经为该对象提取过。
// This is the PFObject
PFObject *object = [PFObject objectWithoutDataWithClassName:@"SomeClass" objectId:@"id"];
// I needed to fetch the first object
[object fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
// Here, I get the second object, inside pointer column
PFObject *object2 = object[@"pointer"];
// Fetch again to get data
[object2 fetchIfNeededInBackgroundWithBlock:^(PFObject *object, NSError *error) {
// get PFFile in image column
PFFile * file = [object2 objectForKey:@"image"];
NSLog(@"url %@", file.url);
// Now continue code to assign image.
}];
}];
如果查询有问题的对象,则可以包含具有指针的键,这样您只能使用一个请求。