【问题标题】:How do you populate a UIImage view with ASIHTTPRequest given @2x?如何使用给定@2x 的 ASIHTTPRequest 填充 UIImage 视图?
【发布时间】:2011-07-18 19:17:06
【问题描述】:

我一直在尝试使用 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

【问题讨论】:

    标签: iphone ios uiimageview uiimage asihttprequest


    【解决方案1】:

    所以事实证明一切都很好。错误是在分配给 UIImageView 之后正在执行完成块。通过将其放在 makeAssignment 方法中,一切正常。以前我只是在那里填充 UIImage,然后将其分配给主线程中的 UIImageView。

    【讨论】:

      【解决方案2】:

      它不会是自动@2x 的东西 - 如果系统找不到附加了“@2x”的图像,它将简单地使用原始图像并将其放大。如果您想验证这一点,只需在模拟器上使用非视网膜显示模式运行应用即可。

      如果您在模拟器上运行您的应用程序,您实际上可以使用查找器浏览缓存目录,以验证您的图像是否符合您的预期。在您的用户目录中,您将转到~/Library/Application Support/iPhone Simulator/[Your SDK version]/Applications/[Your app]/ 以进入您的应用沙箱,并在那里形成应用程序的库/缓存目录。

      另外,你记录了缓存路径:它是什么出来的?你确定这是一条有效的路径吗?同样,如果您在模拟器上,您可以通过尝试在终端/另一个应用程序中打开路径来快速验证这一点。

      至少在我看来,您的代码中没有什么特别错误的地方,所以感觉本地文件路径发生了一些事情。也许您可以将您正在访问的 NSLog 发布到控制台。

      【讨论】:

      • 我检查了本地文件路径,它就在那里(以及正确的视网膜尺寸图像)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多