我可以从URL加载UIImage吗? [英] Can I load a UIImage from a URL?
问题描述
我有一个图像的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 ,然后是转发,然后是 - [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长度]?难道不知道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屋!