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

时间:2014-11-28 07:25:34

标签: ios objective-c arrays for-loop nsmutablearray

我有一个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];

我想将这两个图片网址转换为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时,运行程序时没有任何显示。

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

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

有人可以帮忙吗?我觉得我在某个地方犯了一个愚蠢的错误。

谢谢。

3 个答案:

答案 0 :(得分:0)

将此陈述分解为较小的陈述

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

这里可能存在多个失败点

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

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

答案 1 :(得分:0)

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;
}

答案 2 :(得分:0)

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

首先,创建第二个数组,对于网址路径说paths,对加载的图像说images。我不想将图像对象保留在数组中,只是在加载后立即将其设置为图像视图。

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

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

这里有一些代码片段。

- (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
    });
  }
}