【问题标题】:Loading Images from JSON to Xcode将图像从 JSON 加载到 Xcode
【发布时间】:2012-10-26 14:42:14
【问题描述】:

我在将图像从 json 文件加载到 UIImage - Xcode 中的表格单元格中时遇到了一些困难。我试图将图像从服务器加载到 NSArray 中,然后填充表格视图 UIImage 单元格。我在这里缺少什么吗?

图像位于 SQL 服务器上。

感谢您的帮助。

这是从 PHP 到 Xcode 的服务器输出。 (封面图片)

(
    "13497074790148.jpeg",
    "13494650900147.png",
    "13494606630147.png",
    "13494605220147.jpeg",
    "13494602920147.jpeg",
    "13494601850147.jpeg",
    "13491916300147.jpeg"
)

这是 Xcode 中的代码

NSArray *itemsimages = [[NSArray alloc]initWithArray:[results valueForKeyPath:@"cover_image"]];
self.itemImages = itemsimages;

这是表格单元格中的代码

UIImage *imageitm = [UIImage imageNamed: [self.itemImages objectAtIndex: [indexPath row]]];
cell.itmImage.image = imageitm;
return cell;

【问题讨论】:

  • imageNamed: 专为应用程序包中的图像而设计。图片在哪里?
  • 图像位于 SQL 服务器中。我正在尝试从那里检索它们。
  • @PaulLalonde 图像位于 SQL 服务器上。我正在尝试实现 SDWebImage 来获取这些图像。你有什么建议吗?
  • @exlux15 最佳实践是通过某种网络服务而不是直接从 SQL 服务器检索这些图像。只有 web 服务应该与 SQL 服务器通信。
  • @PaulLalonde 谢谢。目前我有一个从 Xcode 发送到 URL 的请求(带有 GET 方法的 PHP)。 PHP 文件获取所需信息,然后 XCode 从 PHP 获取服务器输出。我将如何更改该流程以实现 Web 服务?

标签: objective-c sql xcode json


【解决方案1】:

您没有将这些图像存储在本地,因此它没有任何要显示的图像。我建议使用SDWebImage 提供从远程位置的异步图像加载+缓存机制。

【讨论】:

  • 感谢您的回复。 SDWebImage 可以用于从 SQL 获取图像还是仅从 URL 获取图像?
  • 如果您访问我提供的链接,您将获得实施它所需的所有信息。您必须提供一个 baseURL。例如myServer.com/123565643.jpeg 将包含您的图像。
  • 我正在尝试获取SQL表中的整列图像数据。这个 baseURL 会得到所有这些数据吗?
  • 如果您可以在浏览器中打开 baseUrl+photoName 并显示您想要的图像,您可以使用它。否则没有。这适用于所有图像。我的意思是您需要图像的 url 才能以这种方式加载它。应该很容易让您获取图片的网址。
【解决方案2】:
-(void) viewDidLoad
{

NSURL *url = [NSURL URLWithString:@"YOUR URL"];

NSData *data = [NSData dataWithContentsOfURL:url];

    NSError *error;

    NSMutableDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSMutableArray *img = [[NSMutableArray alloc]init];
    NSArray *websiteDetails = (NSArray *) [json objectForKey:@"logos"];

    for(int count=0; count<[websiteDetails count]; count++)
    {
        NSDictionary *websiteInfo = (NSDictionary *) [websiteDetails objectAtIndex:count];
        imagefile = (NSString *) [websiteInfo objectForKey:@"image_file"];
        if([imagefile length]>0)
        {
            NSLog(@"Imagefile URL is: %@",imagefile);
            [img addObject:imagefile];
        }
    }
    //NSarray listofURL
    listofURL = img;
}



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

    static NSString *simpleTableIdentifier = @"SimpleTableItem";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

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

    dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //this will start the image loading in bg
    dispatch_async(concurrentQueue, ^{

        NSURL *url = [NSURL URLWithString:[listofURL objectAtIndex:indexPath.row]];
        NSData *image = [[NSData alloc] initWithContentsOfURL:url];

        //this will set the image when loading is finished
        dispatch_async(dispatch_get_main_queue(), ^{

            cell.imageView.image = [UIImage imageWithData:image];
        });
    });
    }
    return cell;
}

【讨论】:

    【解决方案3】:

    您需要在 json 响应中有一个正确的 url,或者您可以将 url 的公共部分存储在代码本身中,然后在稍后附加从服务器返回的图像名称。 我在相同的条件下做了以下操作

        __autoreleasing NSError* error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        NSDictionary *dict = ((NSDictionary *) result)[@"result"];
    
        NSString *url = dict[@"imageURL"];
    
        NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
        UIImage *image = [[UIImage alloc] initWithData:imageData];
    
        [_buttonImageView setImage:image forState:UIControlStateNormal];
    

    其中 data 是从服务器返回的响应。

    【讨论】:

    • 你应该做这个异步,特别是因为它将被用在 tableview 中
    • 是的,你是对的。但我只是想给出这个想法.. 其余的取决于开发人员。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-06
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 2018-04-05
    • 2020-03-23
    相关资源
    最近更新 更多