【问题标题】:Reading string from a URL and then loading it's image in an UIImageView从 URL 读取字符串,然后将其图像加载到 UIImageView
【发布时间】:2011-04-19 22:46:11
【问题描述】:

我有以下代码可以在 UIImageView 上加载我想要从 Internet 获取的图像:

NSString* mapURL = @"http://mydomain.com/image-320x480.png";
NSData* imageData = [[NSData alloc]initWithContentsOfURL:url];

 UIImage* image = [[UIImage alloc] initWithData:imageData];
 [marcaBackground setImage:image];
 [imageData release];
 [image release];

我不想硬编码图像的 URL,而是加载另一个 URL 的字符串,然后在 UIImageView 上加载该图像。

我的网址仅返回另一个文本格式的网址。

例如:http://mydomain.com/url 返回http://mydomain.com/image-320x480.png

我怎样才能完成这项任务?

【问题讨论】:

  • 这听起来很乱。为什么不让http://mydomain.com/url 返回正确的 HTTP 重定向而不是您自己的自定义方案?

标签: iphone cocoa-touch uiimageview nsurl


【解决方案1】:

用途:

NSData *imageData = 
   [[NSData alloc] initWithContentsOfUrl:
       [NSString 
           stringWithContentsOfUrl: 
             [NSURL URLWithString:@"http://mydomain.com"]
           encoding: NSUTF8StringEncoding
           error: nil
       ]
   ];

然后照常进行。

UIImage* image = [[UIImage alloc] initWithData:imageData];
[marcaBackground setImage:image];
[imageData release];
[image release];

【讨论】:

  • 优秀的回复。只需要更正 initWithContentsOfUrl 中 initWithContentsOfURL 的拼写错误。谢谢!
猜你喜欢
  • 1970-01-01
  • 2013-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-26
  • 1970-01-01
相关资源
最近更新 更多