Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/114.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 如何使UIImageView加载图像,但URL位于UILabel上?_Iphone_Ios_Uiimageview_Uilabel - Fatal编程技术网

Iphone 如何使UIImageView加载图像,但URL位于UILabel上?

Iphone 如何使UIImageView加载图像,但URL位于UILabel上?,iphone,ios,uiimageview,uilabel,Iphone,Ios,Uiimageview,Uilabel,如何使UIImageView加载图像,但URL位于UILabel上 更多说明:我有一个包含图像网址的UILabel,因此我希望该地址在UIImageView上加载图像。我想它涉及一些编码,但我不知道如何做到这一点。请帮忙 UIImage *myImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:yourlabel.text]]]; 在UIImageView上添加上述图像

如何使
UIImageView
加载图像,但URL位于
UILabel

更多说明:我有一个包含图像网址的
UILabel
,因此我希望该地址在
UIImageView
上加载图像。我想它涉及一些编码,但我不知道如何做到这一点。请帮忙

UIImage *myImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:yourlabel.text]]];
在UIImageView上添加上述图像:

UIImageView *img = [[UIImageView alloc] initWithImage:myImage];
编辑:去阅读“类别”部分。这里的示例方法检查字符串是否是带有http://前缀的URL。使用它,如果它返回否,只需将字符串“http://”作为前缀添加到yourlabel.text,然后在上面传递它,而不是yourlabel.text

是的,正如链接所说,它只是基本的URL检测,只是验证字符串是否以http作为前缀://

示例代码:一个简单的基于窗口的应用程序

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.

    UILabel *label = [[UILabel alloc] init];
    label.text = @"http://www.arthistoryarchive.com/arthistory/cubism/images/PabloPicasso-Weeping-Woman-with-Handkerchief-1937.jpg";
    UIImage *myImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:label.text]]];
    UIImageView *img = [[UIImageView alloc] initWithImage:myImage];
    img.frame = CGRectMake(0, 0, 768, 1024);
    [self.window addSubview:img];
    [self.window makeKeyAndVisible];
    return YES;
}

这比你想象的要容易

首先,将URL字符串转换为NSURL

NSURL*   imageUrl  = [NSURL URLWithString:url];
然后在该URL加载数据

NSData*  imageData = [NSData dataWithContentsOfURL:imageUrl];
然后将数据加载到UIImage中

UIImage* image     = [UIImage imageWithData:imageData];
最后,告诉UIImageView显示该图像

imageView.image = image;

OP说URL在UILabel上。我请投否决票的人来解释问题所在。是我-我误读了这个问题,认为你的答案是错误的,意识到我的错误并推翻了我的否决票。我理解代码,但它仍然不起作用,你能发布完整的代码以将图像加载到imageView吗!标签中的URL是什么样子的?先发那个。也许你在UILabel中的URL就是问题所在[