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

发布于 2024-12-04 18:49:09 字数 188 浏览 0 评论 0原文

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

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

How can I make an UIImageView load an image, but the URL is on a UILabel?

More explanation: I have a UILabel containing an image web address, so I want this address to load an image on the UIImageView. I suppose it involves some encoding but I have no idea how to do this. Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜点 2024-12-11 18:49:10
UIImage *myImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:yourlabel.text]]];

将上面的图像添加到您的 UIImageView 上:

UIImageView *img = [[UIImageView alloc] initWithImage:myImage];

编辑:转到此处并阅读“类别”部分。那里的示例方法检查您的字符串是否是带有 http:// 前缀的 URL。使用它,如果它返回 NO,只需添加字符串“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;
}
UIImage *myImage = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:yourlabel.text]]];

Add the above image on your UIImageView:

UIImageView *img = [[UIImageView alloc] initWithImage:myImage];

EDIT: Go here and read up on the "Categories" section. The sample method there checks if your string is a URL with a http:// prefix to it. Use that and if it returns NO, just add the string "http://" as a prefix to yourlabel.text and then pass it above instead of yourlabel.text

And yes as the link says, its just basic URL detection that just verifies if the string has been prefixed with http://

Sample code: A simple window based app

- (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;
}
冷心人i 2024-12-11 18:49:10

这比你想象的要容易。

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

NSURL*   imageUrl  = [NSURL URLWithString:url];

然后在该 URL 加载数据

NSData*  imageData = [NSData dataWithContentsOfURL:imageUrl];

然后将该数据加载到 UIImage 中

UIImage* image     = [UIImage imageWithData:imageData];

最后,告诉 UIImageView 显示该图像

imageView.image = image;

It's easier than you think.

First, convert your URL string into an NSURL

NSURL*   imageUrl  = [NSURL URLWithString:url];

Then load the data at that URL

NSData*  imageData = [NSData dataWithContentsOfURL:imageUrl];

Then load that data into a UIImage

UIImage* image     = [UIImage imageWithData:imageData];

And finally, tell your UIImageView to display that image

imageView.image = image;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文