我有一个图像的URL(从UIImagePickerController获取)但我不再拥有内存中的图像(该URL是从以前的应用程序运行中保存的).我可以再次从URL重新加载UIImage吗?
我看到UIImage有一个imageWithContentsOfFile:但我有一个URL.我可以使用NSData的dataWithContentsOfURL:来读取URL吗?
根据@ Daniel的回答,我尝试了以下代码,但它不起作用......
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];
}
Run Code Online (Sandbox Code Playgroud)
当我运行它时控制台显示:
-[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'
Run Code Online (Sandbox Code Playgroud)
查看调用堆栈,我调用URLWithString,调用URLWithString:relativeToURL:,然后是initWithString:relativeToURL:,然后是_CFStringIsLegalURLString,然后是CFStringGetLength,然后是forwarding_prep_0,然后转发,然后调用 - [NSObject doesNotRecognizeSelector].
我的NSString(photoURL的地址是0x536fbe0)没有响应长度的任何想法?为什么它说它没有响应 - [NSURL长度]?难道不知道param是NSString,而不是NSURL吗?
好的,代码的唯一问题是字符串到URL的转换.如果我硬编码字符串,其他一切工作正常.所以我的NSString有问题,如果我无法弄清楚,我想这应该是一个不同的问题.插入此行(我从上面的控制台日志粘贴路径),它工作正常:
photoURL = @"file://localhost/Users/gary/Library/Application%20Support/iPhone%20Simulator/3.2/Media/DCIM/100APPLE/IMG_0004.JPG";
Run Code Online (Sandbox Code Playgroud) 可能重复:
带图像的表视图,慢速加载和滚动
我有一个 从服务器UITableView下载图像UITableViewCells.我发现tableView的滚动速度非常慢.
我认为这可能会下载问题,但我已经意识到下载完成后表格仍然会慢慢滚动,图像图标大小较小.
我搜索了谷歌但找不到任何帮助.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
btnBack.hidden = FALSE;
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.backgroundColor = [UIColor clearColor];
cell.textLabel.font = [UIFont fontWithName:@"Noteworthy" size:17.0];
cell.textLabel.font = [UIFont boldSystemFontOfSize:17.0];
cell.textLabel.textColor = [UIColor blackColor];
cell.textLabel.highlightedTextColor = [UIColor blackColor];
}
cell.textLabel.text = [NSString stringWithFormat:@" %@", [test.arrTitle objectAtIndex:indexPath.row]];
NSString *Path;
Path …Run Code Online (Sandbox Code Playgroud)