UIImage from a URL that change by date
my question is simple, i want to load a UIImage from an URL, but this URL change pro开发者_如何学JAVAgrammatically by date.
Example Today the url is http://www.abc.com/2011-10-13/alfa.jpg
tomorrow is http://www.abc.com/2011-10-14/alfa.jpg
the only thing that change is the date part, how can i figure my app load that "alfa.jpg" at current date everytime i start it?
Thanks!
You should use [NSDate date]
which returns the current date and time according to the device time (assuming your device time doesn't differ from the actual by more than a day). Then you should format the date according to your url. Something like-
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSString* dateString = [formatter stringFromDate:[NSDate date]];
[formatter release];
Use an NSDateFormatter
to generate the part of the string that varies with time.
NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString * datePart = [dateFormatter stringFromDate:[NSDate date]];
NSString * theURLString = [NSString stringWithFormat:@"http://www.abc.com/%@/alfa.jpg", datePart];
You will have to maintain a mechanism to check whether if the image for the day has been downloaded to avoid downloading it again.
Create a template of the url you are using nstring
and a DateFormatter object...
NSDateFormatter *df = [[[NSDateFormatter alloc] init] autorelease];
[df setDateFormat:@"yyyy-MM-dd"];
NSString *todayStr = [df stringFromDate:[NSDate date]];
NSString *todayUrl = [NSString stringWithFormat:@"http://www.abc.com/%@/alfa.jpg", todayStr];
todayUrl
has the url for today!
Its an easy task just format your URL string using current date like,
NSString *URLString = [[NSString alloc] initWithFormat:@"http://www.abc.com/%@/alfa.jpg", currentDate];
Then use this string for URL request.
精彩评论