如何使用给定 @2x 的 ASIHTTPRequest 填充 UIImage 视图?

发布于 2024-10-23 18:28:11 字数 2747 浏览 1 评论 0原文

我一直在尝试使用 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/[电子邮件受保护]

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

I've been trying to load images from a url using ASIHTTPRequest but I always come up with a blank UIImage. I think it might have something to do with iOS automatically choosing the @2x named version of images or vica versa.

[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);

The makeAssignment method is below.

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]);

Nothing I do, including naming images @2x on the server or providing both versions, gets it to load. Any ideas? Has anyone done this before? When I load them locally (from within the package) I don't have any issues.

Thanks!

EDIT

Here's the log output

2011-03-19 11:46:11.088 clv[82974:207] Successful Update

2011-03-19 11:46:12.822 clv[82974:207] http://173.246.100.185/[email protected]

2011-03-19 11:46:12.844 clv[82974:207] >

2011-03-19 11:46:12.913 clv[82974:207] Successful Update

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

音盲 2024-10-30 18:28:11

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

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

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

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

It won't be the automatic @2x thing - if the system can't find an image with '@2x' appended to it it will simply use the original image and scale it up. If you want to verify that, just run the app on the simulator using the non-retina display mode.

If you run your app on the simulator you can actually browse the cache directory using the finder to verify your images are there as you expect. In your user directory, you would go to ~/Library/Application Support/iPhone Simulator/[Your SDK version]/Applications/[Your app]/ to get to your app sandbox, and form there probably into the app's library/cache directory.

Also, you log the cache path: what does it come out as? Are you sure it's a valid path? Again, if you're on the simulator you can quickly verify this by trying to open the path in the terminal/another app.

Nothing in your code looks particularly wrong to me at least, so it feels like there's something happening with the local file path. Perhaps you could post the NSLogs you're getting to the console.

话少心凉 2024-10-30 18:28:11

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

So it turns out that everything was fine. What was wrong is that the completion block was being executed after the assignment to the UIImageView. By placing that in the makeAssignment method everything works. Previously I was just populating the UIImage there and then assigning that to the UIImageView in the main thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文