我可以从 URL 加载 UIImage 吗? [英] Can I load a UIImage from a URL?

查看:19
本文介绍了我可以从 URL 加载 UIImage 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像的 URL(从 UIImagePickerController 获得),但我的内存中不再有图像(URL 是从应用程序的上一次运行中保存的).我可以再次从 URL 重新加载 UIImage 吗?

I have a URL for an image (got it from UIImagePickerController) but I no longer have the image in memory (the URL was saved from a previous run of the app). Can I reload the UIImage from the URL again?

我看到 UIImage 有一个 imageWithContentsOfFile: 但我有一个 URL.我可以使用 NSData 的 dataWithContentsOfURL: 来读取 URL 吗?

I see that UIImage has a imageWithContentsOfFile: but I have a URL. Can I use NSData's dataWithContentsOfURL: to read the URL?

基于@Daniel 的回答,我尝试了以下代码但它不起作用...

based on @Daniel's answer I tried the following code but it doesn't work...

NSLog(@"%s %@", __PRETTY_FUNCTION__, photoURL);     
if (photoURL) {
    NSURL* aURL = [NSURL URLWithString:photoURL];
    NSData* data = [[NSData alloc] initWithContentsOfURL:aURL];
    self.photoImage = [UIImage imageWithData:data];
    [data release];
}

当我运行它时,控制台显示:

When I ran it the console shows:

-[PhotoBox willMoveToWindow:] file://localhost/Users/gary/Library/Application%20Support/iPhone%20Simulator/3.2/Media/DCIM/100APPLE/IMG_0004.JPG
*** -[NSURL length]: unrecognized selector sent to instance 0x536fbe0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSURL length]: unrecognized selector sent to instance 0x536fbe0'

查看调用堆栈,我正在调用 URLWithString,它调用 URLWithString:relativeToURL:,然后是 initWithString:relativeToURL:,然后是 _CFStringIsLegalURLString,然后是 CFStringGetLength,然后是 forwarding_prep_0,然后是 forwarding,然后 -[NSObject doesNotRecognizeSelector].

Looking at the call stack, I'm calling URLWithString, which calls URLWithString:relativeToURL:, then initWithString:relativeToURL:, then _CFStringIsLegalURLString, then CFStringGetLength, then forwarding_prep_0, then forwarding, then -[NSObject doesNotRecognizeSelector].

任何想法为什么我的 NSString(photoURL 的地址是 0x536fbe0)不响应长度?为什么它说它不响应 -[NSURL length]?它不知道param是一个NSString,而不是一个NSURL吗?

Any ideas why my NSString (photoURL's address is 0x536fbe0) doesn't respond to length? Why does it say it doesn't respond to -[NSURL length]? Doesn't it know that param is an NSString, not a NSURL?

好的,代码唯一的问题是字符串到 URL 的转换.如果我对字符串进行硬编码,则其他一切正常.所以我的 NSString 有问题,如果我无法弄清楚,我想这应该作为一个不同的问题.插入这一行(我粘贴了上面控制台日志中的路径),它工作正常:

OK, the only problem with the code is the string to URL conversion. If I hardcode the string, everything else works fine. So something is wrong with my NSString and if I can't figure it out, I guess that should go in as a different question. With this line inserted (I pasted the path from the console log above), it works fine:

photoURL = @"file://localhost/Users/gary/Library/Application%20Support/iPhone%20Simulator/3.2/Media/DCIM/100APPLE/IMG_0004.JPG";

推荐答案

你可以这样做(同步,但紧凑):

You can do it this way (synchronously, but compact):

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:MyURL]]];

更好的方法是使用 Apple 的 LazyTableImages 来保持交互性.

A much better approach is to use Apple's LazyTableImages to preserve interactivity.

这篇关于我可以从 URL 加载 UIImage 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆