ZFUtilities.m 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. //
  2. // ZFUtilities.m
  3. // ZFPlayer
  4. //
  5. // Copyright (c) 2016年 任子丰 ( http://github.com/renzifeng )
  6. //
  7. // Permission is hereby granted, free of charge, to any person obtaining a copy
  8. // of this software and associated documentation files (the "Software"), to deal
  9. // in the Software without restriction, including without limitation the rights
  10. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. // copies of the Software, and to permit persons to whom the Software is
  12. // furnished to do so, subject to the following conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be included in
  15. // all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. #import "ZFUtilities.h"
  25. @implementation ZFUtilities
  26. + (NSString *)convertTimeSecond:(NSInteger)timeSecond {
  27. NSString *theLastTime = nil;
  28. long second = timeSecond;
  29. if (timeSecond < 60) {
  30. theLastTime = [NSString stringWithFormat:@"00:%02zd", second];
  31. } else if(timeSecond >= 60 && timeSecond < 3600){
  32. theLastTime = [NSString stringWithFormat:@"%02zd:%02zd", second/60, second%60];
  33. } else if(timeSecond >= 3600){
  34. theLastTime = [NSString stringWithFormat:@"%02zd:%02zd:%02zd", second/3600, second%3600/60, second%60];
  35. }
  36. return theLastTime;
  37. }
  38. + (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size {
  39. if (!color || size.width <= 0 || size.height <= 0) return nil;
  40. CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
  41. UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
  42. CGContextRef context = UIGraphicsGetCurrentContext();
  43. CGContextSetFillColorWithColor(context, color.CGColor);
  44. CGContextFillRect(context, rect);
  45. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  46. UIGraphicsEndImageContext();
  47. return image;
  48. }
  49. + (NSBundle *)bundle {
  50. static NSBundle *bundle = nil;
  51. static dispatch_once_t onceToken;
  52. dispatch_once(&onceToken, ^{
  53. bundle = [NSBundle bundleWithPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"ZFPlayer" ofType:@"bundle"]];
  54. });
  55. return bundle;
  56. }
  57. + (UIImage *)imageNamed:(NSString *)name {
  58. if (name.length == 0) return nil;
  59. int scale = (int)UIScreen.mainScreen.scale;
  60. if (scale < 2) scale = 2;
  61. else if (scale > 3) scale = 3;
  62. NSString *n = [NSString stringWithFormat:@"%@@%dx", name, scale];
  63. UIImage *image = [UIImage imageWithContentsOfFile:[self.bundle pathForResource:n ofType:@"png"]];
  64. if (!image) image = [UIImage imageWithContentsOfFile:[self.bundle pathForResource:name ofType:@"png"]];
  65. return image;
  66. }
  67. @end