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

Ios Xcode-将数组中的图像URL转换为UIImage,ios,objective-c,arrays,for-loop,nsmutablearray,Ios,Objective C,Arrays,For Loop,Nsmutablearray,我有一个NSMutable数组,其中有两个URL(字符串) 我想将这两个图像URL转换为UIImage for(int i = 0; i < [ImageArray count]; i++) { UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:[ImageArray objectAtIndex:i]]]]; //After co

我有一个NSMutable数组,其中有两个URL(字符串)

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

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];
}
由于程序运行后没有显示任何内容,我怀疑for循环中发生了什么,但我不确定是什么

有人能帮忙吗?我觉得我在某处犯了一个愚蠢的错误


谢谢。

把这句话分成几句话

UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
    [NSURL URLWithString:[ImageArray objectAtIndex:i]]]];
这里可能有多个故障点

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

  • 确保此路径上的映像正确,并且创建的NSData对象确实包含数据

  • 你可以试试。它对我很好用。
    //在可变数组上加载图像url。
    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“,无];
    //从Url获取图像
    对于(int i=0;i<[ImageArray计数];i++)
    {
    UIImage*image=[UIImage imageWithData:[NSData dataWithContentsOfURL:
    [NSURL URLWithString:[ImageArray objectAtIndex:i]];
    //转换后,用新的UIImage对象替换相同的数组
    [ImageArray replaceObjectAtIndex:i with Object:image];
    }
    //在ImageView上加载图像
    int XX=0;
    
    对于(int k=0;k来说,使用唯一的数组存储多种类型的对象是非常不安全的

    首先,创建第二个数组,比如说
    路径
    用于url路径,而
    图像
    用于加载的图像。我不希望将图像对象保留在数组中,而只是在加载后将其设置为图像视图

    其次,您的加载操作可能会占用大量设备资源。为了防止接口长时间冻结,我建议您在单独的队列中执行。您可以阅读有关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
        });
      }
    }
    
    -(void)加载图像从路径:(NSArray*)路径
    {
    
    对于(NSUInteger index=0;indexfirst使用
    UIImage ImageName:
    将图像存储在图像对象中。在将图像分配给ImageView之前,是否确定正在运行for循环?很难看出这些断章取义的代码片段发生了什么。@Anopvaidya您能解释一下吗?for循环应该将e指向图像对象的URL。@mttrb是的,在将图像分配给ImageView之前正在运行。您的问题是创建可变数组。我已经发布了一个答案。请检查。这很有效!我没有分配数组。非常感谢!这对调试有很大帮助。我希望能给您答案,但其他人解决了我的问题t、 不过非常感谢你的帮助。
    UIImage * image = [UIImage imageWithData:[NSData dataWithContentsOfURL:
        [NSURL URLWithString:[ImageArray objectAtIndex:i]]]];
    
    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;
    }
    
    - (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
        });
      }
    }