ios - 从 URL iOS 加载图像

标签 ios

<分区>

我正在尝试从图像 URL 加载图像。我研究和尝试过的所有代码都发现了一些错误,这在很大程度上与新的 ARC 兼容性有关。

我需要将图像加载到 ImageView 中。

感谢任何帮助。

谢谢。

最佳答案

我将从这里改编 Jim Dovey 的回答 Getting Image from URL Objective C :

同步版本

NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://myurl/mypic.jpg"]];
UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageWithData: imageData]];

异步版本

dispatch_async(dispatch_get_global_queue(0,0), ^{
    NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: @"http://myurl/mypic.jpg"]];
    if ( data == nil )
        return;
    dispatch_async(dispatch_get_main_queue(), ^{
        UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageWithData: data]];
    });
});

关于ios - 从 URL iOS 加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22693914/

相关文章:

ios - dyld : Library not loaded Reason: no suitable image found. 是否找到:/private/var/mobile/Containers/Bundle/Application…

ios - Microsoft 应用中心构建失败

ios - 以编程方式更改 Rootviewcontroller 后,以前的 ViewController 是否释放内存?

ios - 如何检查字符串 ios 中的多个单词?

iphone - 检查 iPhone 中的 cookie 是否过期

ios - 如何在 UITableViewCell 中调用 viewDidLoad?

ios - 在我的 UILabel(4 行)的子部分(2 行)上应用 lineBreakMode

ios - Swift 中的三角函数

ios - 如何在 UITableViewCell 中创建信息按钮?

android - 在 Crashlytics 中查看特定问题的所有崩溃