Xcode - 将数组中的图像 URL 转换为 UIImages



我有一个NSMutable数组,里面有2个URL(字符串)。

//An NSMutable Array with 2 Image URLS stored on local folder
ImageArray = [NSMutableArray arrayWithObjects:@"http://localhost/images/
image1.jpg", @"http://localhost/images/image2.jpg", nil];

我想将这两个图像 URL 转换为 UIImages。

for(int i = 0; i < [ImageArray count]; i++)
{
    UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
    [NSURL URLWithString:[ImageArray objectAtIndex:i]]]];
    //After converted, replace the same array with the new UIImage Object
    [ImageArray replaceObjectAtIndex:i withObject: image];
}

我认为以上应该(?但是当我将图像分配给图像视图时,当我运行程序时,什么都没有显示。

ImageView.image = [ImageArray objectAtIndex: 0];
ImageView2.image = [ImageArray objectAtIndex: 1];

由于程序运行后没有任何内容显示,我怀疑 for 循环中发生了什么,但我不确定是什么。

谁能帮忙?我觉得我在某个地方犯了一个愚蠢的错误。

谢谢。

You can try with it. It working fine for me.
// Load image url on a Mutable Array.
NSMutableArray *ImageArray = [[NSMutableArray alloc]initWithObjects:@"http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg",@"http://www.last-video.com/wp-content/uploads/2013/11/superbe-image-de-poissons-sous-l-eau.jpg", nil];
//Getting Images from Url
for(int i = 0; i < [ImageArray count]; i++)
{
    UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
                                              [NSURL URLWithString:[ImageArray objectAtIndex:i]]]];
    //After converted, replace the same array with the new UIImage Object
    [ImageArray replaceObjectAtIndex:i withObject: image];
}
//Load images on ImageView
int XX=0;
for (int k=0; k<[ImageArray count]; k++)
{
    UIImageView *imgVw=[[UIImageView alloc]initWithFrame:CGRectMake(XX, 0, 200, 300)];
    [imgVw setImage:[ImageArray objectAtIndex:k]];
    [self.view addSubview:imgVw];
    XX=XX+200;
}

将此语句分解为较小的语句

UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
    [NSURL URLWithString:[ImageArray objectAtIndex:i]]]];

这里可能存在多个故障点

  1. 创建的 NSURL 对象为 nil(使用 NSString 的编码来替换任何空格,甚至检查路径是否正确)

  2. 确保此路径上的映像正确,并且创建的 NSData 对象确实具有数据

使用唯一的数组来存储多种类型的对象是非常不安全的。

首先,创建第二个数组,例如 url 路径的paths和加载的图像的images。我不喜欢将图像对象保留在数组中,而只是在加载后立即将其设置为图像视图。

其次,加载操作可能会占用大量设备资源。为了防止您的界面长时间冻结,我建议您在单独的队列中执行它。您可以在此处阅读有关Grand Central Dispatch的更多信息。这是非常有用的开发工具。

第三,正如@newgeapps_dev已经说过的,你需要检查你创建的变量。至少检查 NSURL 和 NSData 以了解 nil

下面是一些代码片段。

- (void)loadImagesFromPaths:(NSArray *)paths
{
  for (NSUInteger index=0;index<paths.count;index++)
  {
    NSString *path = paths[index];
    NSURL *url = [NSURL URLWithString:path];
    if (!url) // check url initialized
      continue;
    // load image data asynchronously in a separate queue with a low priority
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
      NSData *imageData = [NSData dataWithContentsOfURL:url];
      if (!data)
        return; // if no data - return from block
      UIImage *image = [UIImage imageWithData:data];
      if (!image)
        return; // if no image provided by url - do nothing
      if (index==0)
        [self.imageView0 setImage:image];
      else if (index==1)
        [self.imageView1 setImage:image];
      else  //.. etc
    });
  }
}

最新更新