[英]How do you populate a UIImage view with ASIHTTPRequest given @2x?
我一直试图使用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] /用户/ jonathantpage /库/应用程序支持/ 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发布到控制台。
因此,事实证明一切都很好。 错误的是,在分配给UIImageView之后,正在执行完成块。 通过将其放在makeAssignment方法中,一切正常。 以前,我只是在其中填充UIImage,然后将其分配给主线程中的UIImageView。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.