我有一个图像的URL(从UIImagePickerController获得),但我的内存中不再有该图像(该URL是从之前运行的应用程序中保存的).我可以再次从URL重新加载UIImage吗?
我看到UIImage有一个imageWithContentsOfFile:但是我有一个URL.我可以使用NSData的dataWithContentsOfURL:来读取URL吗?
EDIT1
根据@Daniel的回答,我try 了以下代码,但它不起作用……
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];
}
当我运行它时,控制台显示:
-[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 DoesNotReconficateSelector].
你知道为什么我的NSString(photoURL的地址是0x536fbe0)不响应长度吗?为什么它说它不响应-[NSURL长度]?难道它不知道param是一个NSString,而不是一个NSURL吗?
EDIT2
好的,代码的唯一问题是字符串到URL的转换.如果我对字符串进行硬编码,其他一切都可以正常工作.所以我的NSString有点问题,如果我不能弄清楚,我想这应该是另一个问题.插入这一行(我从上面的控制台日志(log)中粘贴了路径)后,效果很好:
photoURL = @"file://localhost/Users/gary/Library/Application%20Support/iPhone%20Simulator/3.2/Media/DCIM/100APPLE/IMG_0004.JPG";