从细节ScrollView中的Parse中获取图像

时间:2014-03-04 15:58:41

标签: ios image uiscrollview parse-platform horizontalscrollview

好的,我真的很想找到一个例子或教程,告诉我如何实现我正在寻找的东西,但没有任何运气。

我有一个PFQueryTableView,它将数据传递给DetailView(那里都很好)。

我的DetailView是一个水平ScrollView,可以从单击的单元格中获取图像。这就是我的问题:我设法将数据传递给DetailView,但我不知道如何在ScrollView中设置图像。任何人都可以向我发送教程的方向或通过这个问题帮助我吗?

这是我的代码:(显然我错过了设置要在ScrollView中查看的图像的部分。)

BellezaTableViewController.m

#import "BellezaTableViewController.h"
#import "BellezaDetailViewController.h"
#import "BellezaView.h"

@interface BellezaTableViewController ()

@end

@implementation BellezaTableViewController {

}


- (id)initWithCoder:(NSCoder *)aCoder
{
self = [super initWithCoder:aCoder];
if (self) {
    self.parseClassName = @"BellezaView";
    self.textKey = @"cellTitle";
    self.textKey = @"descriptionTitle";

    self.pullToRefreshEnabled = YES;
    self.paginationEnabled = NO;
    self.loadingViewEnabled = YES;
}

return self;

}

- (void)viewDidLoad
{
[super viewDidLoad];

}

- (void)viewDidUnload
{
[super viewDidUnload];
}


- (PFQuery *)queryForTable{

PFQuery *query = [PFQuery queryWithClassName:self.parseClassName];
query.cachePolicy = kPFCachePolicyCacheThenNetwork;


return query;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

static NSString *simpleTableIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}

UILabel *cellTitle = (UILabel*) [cell viewWithTag:101];
cellTitle.text = [object objectForKey:@"cellTitle"];

UILabel *descriptionTitle = (UILabel*) [cell viewWithTag:102];
descriptionTitle.text = [object objectForKey:@"descriptionTitle"];

return cell;
}


- (void) objectsDidLoad:(NSError *)error
{
[super objectsDidLoad:error];

}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"showBellezaDetail"]) {
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    BellezaDetailViewController *destViewController = segue.destinationViewController;

    PFObject *object = [self.objects objectAtIndex:indexPath.row];


    BellezaView *bellezaView = [[BellezaView alloc] init];
    bellezaView.cellTitle = [object objectForKey:@"cellTitle"];
    bellezaView.descriptionTitle = [object objectForKey:@"descriptionTitle"];
    bellezaView.image_1 = [object objectForKey:@"image_1"];
    destViewController.bellezaView = bellezaView;

}
}

@end

BellezaTableViewController.h

#import <UIKit/UIKit.h>
#import <Parse/Parse.h>

@interface BellezaTableViewController : PFQueryTableViewController

@end

DetailViewController.m

#import "BellezaDetailViewController.h"
#import "BellezaView.h"

@interface BellezaDetailViewController ()

@end

@implementation BellezaDetailViewController

@synthesize lookPhoto, bellezaView, activityIndicator, scrollView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}


 - (void)viewDidLoad
{
[super viewDidLoad];{

    [activityIndicator startAnimating];
    [activityIndicator performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:60];

    [scrollView setAlwaysBounceHorizontal:YES];
    [scrollView setAlwaysBounceVertical:NO];
    [scrollView setPagingEnabled:YES];

    NSInteger numberOfViews = 3;
    for (int i = 0; i < numberOfViews; i++){

这将是我不知道如何处理的部分,并且找不到任何要遵循的示例。我应该使用阵列吗?如果是这样,如果我应该从PFQueryTable传递数据,我该如何检索数据?我找到了一些像这段代码获得图像的例子:

image.image = [UIImage imageNamed:[NSString stringWithFormat:@“image_%d”,i + 1]];

但我的问题是我的图片必须通过解析来获取。那我该怎么做?请帮忙!

        lookPhoto.file = bellezaView.image_1;
        [lookPhoto loadInBackground];


        [scrollView addSubview:lookPhoto];

    }

        scrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);


    }
  }


- (void)viewDidUnload {

[self setLookPhoto:nil];

[super viewDidUnload];

}


- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

DetailViewController.h

#import <UIKit/UIKit.h>
#import "BellezaView.h"
#import <Parse/Parse.h>

@interface BellezaDetailViewController : UIViewController

@property (weak, nonatomic) IBOutlet PFImageView *lookPhoto;

@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityIndicator;

@property (nonatomic, strong) BellezaView *bellezaView;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

BellezaView.m

#import "BellezaView.h"

@implementation BellezaView

@synthesize cellTitle, descriptionTitle, image_1;

@end

BellezaView.h

#import <Foundation/Foundation.h>
#import <Parse/Parse.h>

@interface BellezaView : NSObject 

@property (nonatomic, strong) NSString *cellTitle;
@property (nonatomic, strong) NSString *descriptionTitle;

@property (nonatomic, strong) PFFile *image_1;

@end

提前致谢!

2 个答案:

答案 0 :(得分:0)

如果您在PFObject中有图像链接作为属性,则可以执行类似

的操作
PFFile* imageFile = [object objectForKey:@"imageFile"];
UIImage* img = nil;
if (startImage != nil)
{
    img = [[UIImage alloc] initWithData:[imgFile getData]];
}

属性“imageFile”链接到Parse

中的图像

请记住,这会进行网络通话,因此您不应在主线程上执行此操作

答案 1 :(得分:0)

找到方法,这是我的DetailViewController.m文件的代码: 希望它能帮助任何有需要的人! :)

- (void)viewDidLoad {

[super viewDidLoad];
//Do any additional setup after loading the view.

[activityIndicator startAnimating];
[activityIndicator performSelector:@selector(removeFromSuperview) withObject:nil afterDelay:60];


scrollView.pagingEnabled = YES;
[UIView animateWithDuration:20 animations:^{ScrollNext.alpha = 0.0;}];


NSInteger numberOfViews = 10;
for (int i = 0; i < numberOfViews; i++) {


    PFImageView *lookView1 = [[PFImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];

    lookView1.image = [UIImage imageNamed:@"Lading.jpg"];
    lookView1.file = bellezaView.image_1;

    [scrollView addSubview:lookView1];
    [lookView1 loadInBackground:^(UIImage *image, NSError *error) {
        if (!error) {

    lookView1.frame = CGRectMake(0,0,self.view.frame.size.width, self.view.frame.size.height);

    lookView1.contentMode = UIViewContentModeScaleAspectFit;
    scrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);

            }
        }];

    PFImageView *lookView2 = [[PFImageView alloc] init];
    lookView2.frame = CGRectMake(lookView1.frame.size.width*1, 0, 320, 500);

    lookView2.file = bellezaView.image_2;

    [scrollView addSubview:lookView2];
    [lookView2 loadInBackground:^(UIImage *image, NSError *error) {
        if (!error) {

    lookView2.contentMode = UIViewContentModeScaleAspectFit;
    lookView2.frame = CGRectMake(lookView1.frame.size.width*1, 0, self.view.frame.size.width, self.view.frame.size.height);

            }
        }];

    PFImageView *lookView3 = [[PFImageView alloc] init];
    lookView3.frame = CGRectMake(lookView1.frame.size.width*2, 0, 320, 500);

    lookView3.file = bellezaView.image_3;

    [scrollView addSubview:lookView3];
    [lookView3 loadInBackground:^(UIImage *image, NSError *error) {
        if (!error) {

    lookView3.contentMode = UIViewContentModeScaleAspectFit;
    lookView3.frame = CGRectMake(lookView1.frame.size.width*2, 0, self.view.frame.size.width,     self.view.frame.size.height);

            }
        }];

        //Insert Another View

   }
}