【问题标题】:JSON Image View Showing Object at same IndexJSON图像视图在同一索引处显示对象
【发布时间】:2014-10-02 08:57:33
【问题描述】:

我在 UIView 旁边有一个 UIImageView,它在 JSON 数组中显示一个对象 @"photos",如下所示:http://topmobiletrends.com/wp-content/uploads/2013/10/screen568x568-14.jpeg

我的 NSLog 告诉我所有对象都已正确解析。但是UIViews里面的UIImageViews显示的是同一张图片,我相信它是第一个对象[0]。我需要让视图显示@“photos”键的所有对象的每个图像。

这是我的 ViewController.m 代码:​​

。 . .

@interface ViewController ()



{

   NSInteger index;

}

//@property (nonatomic, weak)NSURL *imageURL;



@end

@implementation ViewController

@synthesize menuButton;
@synthesize myImage;
@synthesize priceLabel;

-(void)viewDidLoad{

[super viewDidLoad];



NSURL *bburl = [NSURL URLWithString:@"http://www.suchandsuch.com/api/sites"];
NSData *jsonData = [NSData dataWithContentsOfURL:bburl];

NSURLSession *session = [NSURLSession sharedSession];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:bburl];
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:bburl completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error){
    NSLog(@"%@", response);

    NSData *data = [[NSData alloc]initWithContentsOfURL:location];

    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];

    NSString *imageURLString = [[[dataDictionary objectForKey:@"sites"] objectAtIndex:index] objectForKey:@"photo"];


   NSURL *imageURL = [NSURL URLWithString:[imageURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSLog(@"sites : %@", imageURLString);


   NSArray *sitesArray = [dataDictionary objectForKey:@"sites"];


    NSMutableArray *photos = [[NSMutableArray alloc]initWithArray:sitesArray];

    for (NSDictionary *item in sitesArray) {

        MyObject *current = [MyObject alloc];
        current.name = [item objectForKey:@"name"];
        current.url = [item objectForKey:@"url"];
        current.price = [item objectForKey:@"price"];
        current.photo = [item objectForKey:@"photo"];



       [photos addObject:current];
        NSLog(@"%@", current.photo);

       }


        for (MyObject *photo in photos)

    {

        dispatch_async(dispatch_get_main_queue(),^{

            UIView *dView = [[UIView alloc]initWithFrame:CGRectMake(30,30,258,229)];

            UIImage *image = [UIImage imageWithData:data];
            UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
           imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];

            imageView.frame = CGRectMake(30, 30, 258, 229);

           //Get image from url
           [self.imageView setImageWithURL:imageURL];

           //[self.myImage addSubview:imageView];

           [dView addSubview:imageView];
           [self.view addSubview:dView];
          // priceLabel.text = [[[dataDictionary objectForKey:@"sites"] objectAtIndex:index] objectForKey:@"price"];

           });
        }


   }];

        [task resume];

}

【问题讨论】:

  • 也许 for 循环可以解决问题?...

标签: ios arrays json indexing uiimageview


【解决方案1】:

代码存在一些问题。

  1. 您正在将 NSURL 和名称字符串都添加到照片数组中,但在阅读时您希望它们都是照片。 我建议创建一个名为 Photo 的数据结构,其中包含

@interface PhotoClass :NSObject

@property (nonatomic, strong) NSString *name;

@property (nonatomic, strong) NSURL *url;

@property (nonatomic, strong) NSString *price

@property (nonatomic, strong) NSString *photo;
@end

这样您在解析数据时可以执行以下操作。

PhotoClass *current = [PhotoClass alloc];
current.name = [item objectForKey:@"name"];
current.url = [item objectForKey:@"url"];
current.price = [item objectForKey:@"price"];
current.photo = [item objectForKey:@"photo"];

// Add to array
[photos addObject:current];

然后当您准备好显示时,使用每个 PhotoClass 对象的 url。

代码中的下一个问题是图像视图设置。首先,您需要创建尽可能多的 ImageView。每一个都需要相互偏移才能在该图像后面可见。另外,图像视图似乎以许多不同的方式设置。这只是一个错字吗? :

for (PhotoClass *photo in photos)
{

    dispatch_async(dispatch_get_main_queue(),^{

        UIView *dView = [[UIView alloc]initWithFrame:CGRectMake(50,50,258,229)];

        // The frame of each of the frames needs to be offset a little .. 
        // so you need to figure out how to calculate that for each image.
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:frame];
        imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:photo.url]];
       [dView addSubview:imageView];
       [self.view addSubview:dView];
    ....

    }];
}

在循环结束时,您应该可以看到与照片中的对象一样多的图像视图。

免责声明 - 代码未编译并直接输入到 stackoverflow 浏览器中

【讨论】:

  • 刚试过。图像视图仍然显示相同的图像。我不确定为什么会这样。无论如何感谢您的帮助。
  • 索引的价值在哪里?您需要显示所有代码.. 不让我删除 objectatindex 是什么意思..
  • 我的意思是我试图从 NSString 中删除 objectatindex * imageURLString = [[[dataDictionary objectForKey:@"sites"]objectAtIndex:index]objectForKey:@"photo"];
  • 您需要知道数据是如何存储在您的字典中的,然后您必须正确地阅读......而不是反复试验...... dataDictionary 包含什么?
  • siteArray 是什么样子的..?让我们把它移到这里chat.stackoverflow.com/rooms/59036/imageissue。你为什么还在这样做 - UIImage *image = [UIImage imageWithData:data]; UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];这是错误的,因为您正在创建图像三次
猜你喜欢
  • 2018-11-16
  • 2015-10-17
  • 2019-12-19
  • 1970-01-01
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多