T1Helper.m 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // T1Helper.m
  3. // YYKitExample
  4. //
  5. // Created by ibireme on 15/10/10.
  6. // Copyright (C) 2015 ibireme. All rights reserved.
  7. //
  8. #import "T1Helper.h"
  9. @implementation T1Helper
  10. + (NSBundle *)bundle {
  11. static NSBundle *bundle;
  12. static dispatch_once_t onceToken;
  13. dispatch_once(&onceToken, ^{
  14. NSString *path = [[NSBundle mainBundle] pathForResource:@"ResourceTwitter" ofType:@"bundle"];
  15. bundle = [NSBundle bundleWithPath:path];
  16. });
  17. return bundle;
  18. }
  19. + (YYMemoryCache *)imageCache {
  20. static YYMemoryCache *cache;
  21. static dispatch_once_t onceToken;
  22. dispatch_once(&onceToken, ^{
  23. cache = [YYMemoryCache new];
  24. cache.shouldRemoveAllObjectsOnMemoryWarning = NO;
  25. cache.shouldRemoveAllObjectsWhenEnteringBackground = NO;
  26. cache.name = @"TwitterImageCache";
  27. });
  28. return cache;
  29. }
  30. + (UIImage *)imageNamed:(NSString *)name {
  31. if (!name) return nil;
  32. UIImage *image = [[self imageCache] objectForKey:name];
  33. if (image) return image;
  34. NSString *ext = name.pathExtension;
  35. if (ext.length == 0) ext = @"png";
  36. NSString *path = [[self bundle] pathForScaledResource:name ofType:ext];
  37. if (!path) return nil;
  38. image = [UIImage imageWithContentsOfFile:path];
  39. image = [image imageByDecoded];
  40. if (!image) return nil;
  41. [[self imageCache] setObject:image forKey:name];
  42. return image;
  43. }
  44. + (NSString *)stringWithTimelineDate:(NSDate *)date {
  45. if (!date) return @"";
  46. static NSDateFormatter *formatterFullDate;
  47. static dispatch_once_t onceToken;
  48. dispatch_once(&onceToken, ^{
  49. formatterFullDate = [[NSDateFormatter alloc] init];
  50. [formatterFullDate setDateFormat:@"M/d/yy"];
  51. [formatterFullDate setLocale:[NSLocale currentLocale]];
  52. });
  53. NSDate *now = [NSDate new];
  54. NSTimeInterval delta = now.timeIntervalSince1970 - date.timeIntervalSince1970;
  55. if (delta < -60 * 10) { // local time error
  56. return [formatterFullDate stringFromDate:date];
  57. } else if (delta < 60) {
  58. return [NSString stringWithFormat:@"%ds",(int)(delta)];
  59. } else if (delta < 60 * 60) {
  60. return [NSString stringWithFormat:@"%dm", (int)(delta / 60.0)];
  61. } else if (delta < 60 * 60 * 24) {
  62. return [NSString stringWithFormat:@"%dh", (int)(delta / 60.0 / 60.0)];
  63. } else if (delta < 60 * 60 * 24 * 7) {
  64. return [NSString stringWithFormat:@"%dd", (int)(delta / 60.0 / 60.0 / 24.0)];
  65. } else {
  66. return [formatterFullDate stringFromDate:date];
  67. }
  68. }
  69. + (NSString *)shortedNumberDesc:(NSUInteger)number {
  70. if (number <= 999) return [NSString stringWithFormat:@"%d",(int)number];
  71. if (number <= 9999) return [NSString stringWithFormat:@"%d,%3.3d",(int)(number / 1000), (int)(number % 1000)];
  72. if (number < 1000 * 1000) return [NSString stringWithFormat:@"%.1fK", number / 1000.0];
  73. if (number < 1000 * 1000 * 1000) return [NSString stringWithFormat:@"%.1fM", number / 1000.0 / 1000.0];
  74. return [NSString stringWithFormat:@"%.1fB", number / 1000.0 / 1000.0 / 1000.0];
  75. }
  76. @end