0

我在 MYSQL 数据库表中存储了一个 URL 列表,并且我已经能够通过 JSON 成功检索 URL。

但是,现在我有了 JSON,我不确定如何在我的 iPhone 应用程序的图库类型视图中显示图像。

我计划拥有的是在数据库中图像的大量 URL 列表,我想一次显示 x 个数量,然后一旦用户滚动到最后,我想再显示 x 个数量。喜欢 pinterest。

有什么提示吗?

#import "mySQLAppViewController.h"


@interface mySQLAppViewController (){

}

- (IBAction)loadJSON:(id)sender;

@end


@implementation mySQLAppViewController

- (IBAction)loadJSON:(id)sender {

    NSString *dataUrl = [NSString stringWithFormat:@"http://MYDOMAIN.com/results.php"];
    NSURL *url = [NSURL URLWithString:dataUrl];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFJSONRequestOperation *operation =
    [AFJSONRequestOperation JSONRequestOperationWithRequest:request
            success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {

               for (int i =0 ; i < [JSON count]; i++) {

                   //NSString *image_url = JSON[i][@"image_url"];
                   //NSLog(@"%@", image_url);
                   //^^THIS SUCESSFULLY LOGS THE URL LINKS (I.E. HTTP://MYDOMAIN.COM/CAR.JPG)

                   NSURL *imageURL = [NSURL URLWithString:JSON[i][@"image_url"]];
                   NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
                   UIImage *image = [UIImage imageWithData:imageData];
                   UIImageView *imageView = [[UIImageView alloc] initWithImage:image];


               }

            }

            failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                UIAlertView *av = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                             message:[NSString stringWithFormat:@"%@",error]
                                                            delegate:nil
                                                   cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [av show];
            }];

    [operation start];

}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

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

@end
4

2 回答 2

2

听起来你想看看 UICollectionView。这是一个帮助您入门的教程: 在 iOS 6 中开始 UICollectionView

于 2013-06-04T13:10:52.673 回答
1

编辑:在 davidm 的回答更完整之后编辑:(https://stackoverflow.com/a/16918758/412339)如果它(UICollectionView)最适合您的解决方案,请将其标记为答案。

iOS 6 及更高版本:

你真的应该看看 UICollectionView。在这里可以找到一个非常好的资源:Create an iOS 6 UICollectionView Using Storyboards

iOS 5 及更低版本:

您可以做的一件事是创建一个自定义 ViewController,其中包含一组图像 URL 或 JSON 数组:

@property (nonatomic, retain) NSArray *imageURLs;

然后创建一组可重用的 UIImageViews(这本身就是另一个问题,但基本上你需要一个数组来保存具有重用标识符的自定义 UIImageViews,类似于 UITableViewCell)。

@property (nonatomic, retain) NSArray *imageViews;

最后,您将需要一个 UIScrollView 和 UIView(包含 UIImageViews)来监视位置并使用UIScrollViewDelegate方法在屏幕上添加图像视图(scrollViewDidScroll: 应该可以工作)。

或者,您可以使用自定义 UITableViewCell 实现 UITableViewController 和 Delegate,让 Apple 的工程师为您处理大部分工作,但您无法并排放置照片,只能堆叠在一起。这可能是你想要的。

于 2013-06-04T06:17:11.450 回答