简体   繁体   中英

How do you populate a UIImage view with ASIHTTPRequest given @2x?

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/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] 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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM