【问题标题】:Image fetched from webservice is not displaying in viewcontroller从 web 服务获取的图像未显示在视图控制器中
【发布时间】:2013-09-17 09:34:27
【问题描述】:

我尝试获取图像位置,但它没有在 UIImageView 中显示,用于获取的代码是,

在class.h文件中,

@property (strong, nonatomic) IBOutlet UIImageView *imageview;

class.m 文件,

NSString *imagee;

imagee = [result objectForKey:@"image"];
NSLog(@"image is %@ \n",imagee);

“imagee”字符串可以返回图片的准确网址。

图像显示代码是,

UIImage *imagevieww = [UIImage imageNamed:[result objectForKey:@"image"]];
imageview.image = imagevieww;

但最后我可以得到应该放置图像的空白空间。

【问题讨论】:

  • 一次尝试这样。 UIImage *imagevieww = [UIImage imageNamed:[NSString stringWithFormat:@"%@",[result objectForKey:@"image"]]];
  • 一旦你可以在 NSLog 中打印 imagevieww 对象。
  • @kalyanipuvvada,我仍然可以在 imageview 上获得空白
  • @kalyanipuvvada,我可以在控制台上打印 .jpg 文件。

标签: ios objective-c uiimageview imageview


【解决方案1】:

找到了我的问题的答案,感谢所有帮助我找到这个答案的人。

更新:在 urlstring 中包含“http”,以便它可以从 web 服务中获取正确的图像。

 NSString *urlString = [NSString stringWithFormat:@"http://%@",[result objectForKey:@"image"]];
    NSLog(@"url image is %@ \n",urlString);

    NSData *imageData = [[NSData alloc] init];

   imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];

    NSLog(@"image data is %@",imageData);

    imageview.image = [UIImage imageWithData:imageData];

【讨论】:

    【解决方案2】:

    UIImage 方法imageNamed: 用于加载本地存在的图像。在您的情况下,您需要从 URL 下载图像。根据需要,有多种方法可以做到这一点。

    最简单的是,

    //URL encode the string
    NSString *url = [[result objectForKey:@"image"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
    imageview.image = [UIImage imageWithData:imageData];
    

    这将在主线程(如果从主线程调用)同步下载图像。您想在后台线程上异步执行此操作。通常dispatch_async(GCD) 用于执行此操作。

    希望有帮助!

    【讨论】:

    • 我仍然只能得到空的图像视图。
    • @sathya 你确定[result objectForKey:@"image"] 返回一个有效的 URL 或任何 URL?您可以记录值并检查吗?
    • @sathya 您的IBOutlet 是否正确连接到imageview 变量?下载图片时imageData是否等于nil
    • @sathya 这意味着该 URL 没有下载数据。网址有问题。
    • Amar,我已经找到解决前一天遇到的问题的方法,imageData 的“null”值是因为缺少附加“http”和获取所需的 url。发布了我的问题的答案...
    【解决方案3】:

    如果 'imagee' 返回了准确的 url ,那么从 web 服务获取图像应该是这样的。

    imageview.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[result objectForKey:@"image"]]]];
    

    【讨论】:

    • 在尝试了建议的代码后,我可以得到空的图像视图
    • 你能告诉我你得到的远程 URL 吗?我的意思是'imagee'字符串。
    【解决方案4】:

    试试这个

    NSString *imageString = [NSString stringWithFormat:@"data:image/jpg;base64,%@",[yourDict objectForKey:@"ImageString"]];
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:imageString]];
    UIImage *image = [UIImage imageWithData:imageData];
    

    【讨论】:

      【解决方案5】:

      .h

      @property (strong, nonatomic) IBOutlet UIImageView *imageView;
      

      .m

      从 Web 服务获取图像:

      NSURL *url = [NSURL URLWithString:@"http://www.site.com/image.jpg"];
      NSData *myImageData = [NSData dataWithContentsOfURL:url];        
      UIImage *image = [[UIImage alloc] initWithData:myImageData];
      

      将图像设置为 ImageView:

      imageView.image = image;
      

      【讨论】:

        【解决方案6】:

        我做了以下,它对我有用。

        NSURL *url = [NSURL URLWithString:@"http://www.yourdomain.com/imagename.png"];
        NSData *myData = [NSData dataWithContentsOfURL:url];
        UIImage *image = [[UIImage alloc] initWithData:myData];
        ["%storyboardImageName%" setImage:image];
        

        请注意,storyboardImagename 是您放在 xib 或故事板上的变量。您将需要更改它。还要注意实际上将它们联系在一起的“setImage”属性。

        希望这会有所帮助。

        【讨论】:

          【解决方案7】:

          URL 转换为UIImage

          试试这个:

          NSURL *url = [NSURL URLWithString:@"http://yoursite.com/imageName.png/jpg"];
          
          NSData *myData = [NSData dataWithContentsOfURL:url];
          
          UIImage *image = [[[UIImage alloc] initWithData:myData] autorelease];
          

          在 imageview 上使用此图像。如果您遇到问题,请尝试在后台下载图像。 Follow this Link

          【讨论】:

          • 检查浏览器中的url是否正常。
          • 是的,它在浏览器中工作正常...可以得到所需的确切图像
          • 你能给你的网址试试吗?也试试我在编辑中建议的链接。
          猜你喜欢
          • 2013-09-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-02-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多