【问题标题】:image downloading from URL in iPhone application从 iPhone 应用程序中的 URL 下载图像
【发布时间】:2010-08-12 11:57:54
【问题描述】:

我在 iPhone 应用程序中使用谷歌图表功能....我想从http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World 下载图像并将其放入 UIImageView ... 是否有任何示例应用程序可以执行此类功能?或任何其他方式在应用程序中显示图表?

提前谢谢...

【问题讨论】:

    标签: iphone objective-c uiimageview uiimage


    【解决方案1】:

    这会阻塞,使用 NSURLConnection 进行非阻塞下载

    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World"]];
    
    UIImage *downloadedImage = [UIImage imageWithData:imageData];
    
    myImageView.image = downloadedImage;
    

    【讨论】:

      【解决方案2】:

      试试imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:aURL]]

      【讨论】:

        【解决方案3】:

        @iSwap - 我知道你的代码为什么不起作用。

        我认为您将 url 作为字符串而不是 url。 如上代码----

        imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:aURL]];
        

        有一个URL应该是一个url而不是一个字符串,你把它写成一个字符串

        imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:@"http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World"]];
        

        您忘记将此字符串转换为 url。

        正确的方法应该是这样的-

        imageView.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://chart.apis.google.com/chart?cht=p3&chd=t:60,40&chs=250x100&chl=Hello|World"]]];
        

        我希望你明白了,还有一件事是“willcodejavaforfood”和“CodeBrickie”代码之间没有区别。只是写作方式略有不同。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2021-10-05
          • 2011-07-02
          • 1970-01-01
          • 1970-01-01
          • 2014-06-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多