我一直在尝试使用ASIHTTPRequest从URL加载图像,但是我总是想出一个空白的UIImage。我认为这可能与iOS自动选择@ 2x命名版本的图像有关,反之亦然。
[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];
NSString *url_string = [NSString stringWithFormat:@"http://173.246.100.185/%@", [eventDictionary objectForKey:kEventDescriptionImageURLKey]];
NSURL *url = [NSURL URLWithString:url_string];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy];
[request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy];
[request setSecondsToCache:86400];
[request setDelegate:self];
[request setCompletionBlock:^{
NSLog(@"Successful Update");
[self makeAssignment];
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(@"%@", [error localizedDescription]);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Update Failed"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}];
[request startAsynchronous];
NSLog(@"%@", url_string);
makeAssignment方法如下。
NSString *url_string = [NSString stringWithFormat:@"http://173.246.100.185/%@", [eventDictionary objectForKey:kEventDescriptionImageURLKey]];
NSURL *url = [NSURL URLWithString:url_string];
downloadedImage = [[UIImage alloc] initWithContentsOfFile:[[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:url]];
NSLog(@"%@", downloadedImage);
NSLog(@"%@", [[ASIDownloadCache sharedCache] pathToCachedResponseDataForURL:url]);
我没有做任何事情,包括在服务器上命名图像@ 2x或提供两个版本,都无法加载它。有任何想法吗?有人做过吗?当我从包装中本地加载它们时,我没有任何问题。
谢谢!
编辑
这是日志输出
2011-03-19 11:46:11.088 clv [82974:207]成功更新
2011-03-19 11:46:12.822 clv [82974:207] http://173.246.100.185/ying_yang_concert@2x.png
2011-03-19 11:46:12.844 clv [82974:207]>
2011-03-19 11:46:12.913 clv [82974:207]成功更新
2011-03-19 11:46:12.932 clv [82974:207]
2011-03-19 11:46:12.932 clv [82974:207] / Users / jonathantpage / Library / Application Support / iPhone Simulator / 4.3 / Applications / A17C0938-D2ED-447C-BD17-94726C5E5A66 / Library / Caches / ASIHTTPRequestCache / PermanentStore /FE05295C8CD7687DC7A505C9070B6FC7.png
最佳答案
它不会是自动的@ 2x东西-如果系统找不到添加了'@ 2x'的图像,它将仅使用原始图像并将其放大。如果要验证,只需使用非视网膜显示模式在模拟器上运行该应用即可。
如果您在模拟器上运行应用程序,则实际上可以使用查找器浏览缓存目录,以验证图像是否在您期望的位置。在用户目录中,您将转到~/Library/Application Support/iPhone Simulator/[Your SDK version]/Applications/[Your app]/
进入应用程序沙箱,然后在其中形成应用程序的library / cache目录。
此外,您还记录了缓存路径:它的结果是什么?您确定这是有效路径吗?同样,如果您使用的是模拟器,则可以通过尝试在终端/另一个应用程序中打开路径来快速验证这一点。
至少我看来,您的代码中没有什么特别出错,因此,感觉本地文件路径中正在发生某些事情。也许您可以将要获取的NSLogs发布到控制台。
关于iphone - 如何使用给定的@ 2x的ASIHTTPRequest填充UIImage View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5363180/