RAHomeOrderModel.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // RAHomeOrderModel.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/6/1.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RAHomeOrderModel.h"
  9. #import "UIImage+RedAnt.h"
  10. #import "NSData+RAImageType.h"
  11. @implementation RAHomeOrderModel {
  12. UIImage *_icon;
  13. UIColor *_orderTypeColor;
  14. }
  15. - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
  16. }
  17. - (UIImage *)icon {
  18. return _icon;
  19. }
  20. - (UIColor *)orderTypeColor {
  21. if (!_orderTypeColor) {
  22. _orderTypeColor = [UIColor blackColor];
  23. }
  24. return _orderTypeColor;
  25. }
  26. - (void)setColor:(NSString *)color {
  27. _color = color;
  28. if ([color hasPrefix:@"#"]) {
  29. color = [color stringByReplacingOccurrencesOfString:@"#" withString:@"0x"];
  30. }
  31. if (![color hasPrefix:@"0x"]) {
  32. _orderTypeColor = nil;
  33. }
  34. _orderTypeColor = UIColorFromRGB(strtoul([color UTF8String], 0, 16));
  35. }
  36. - (void)setBackendFlag:(BOOL)backendFlag {
  37. _backendFlag = backendFlag;
  38. if (self.delegate && [self.delegate respondsToSelector:@selector(refreshUI)]) {
  39. dispatch_async(dispatch_get_main_queue(), ^{
  40. [self.delegate refreshUI];
  41. });
  42. }
  43. }
  44. - (void)setIcon:(UIImage *)icon {
  45. _icon = icon;
  46. // _icon = [UIImage imageNamed:@"on_the_way"];
  47. if (self.delegate && [self.delegate respondsToSelector:@selector(refreshUI)]) {
  48. dispatch_async(dispatch_get_main_queue(), ^{
  49. [self.delegate refreshUI];
  50. });
  51. }
  52. }
  53. //- (void)setValuesForKeysWithDictionary:(NSDictionary<NSString *,id> *)keyedValues {
  54. // [super setValuesForKeysWithDictionary:keyedValues];
  55. //
  56. // [self setIconURL:@"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1536134884&di=9ea523019212b373ac04f4411e8aa56b&imgtype=jpg&er=1&src=http%3A%2F%2Fp3.music.126.net%2Fyp-UZgqWp6FV_AN4hPozjQ%3D%3D%2F109951163414711232.jpg"];
  57. //}
  58. - (void)setIconURL:(NSString *)iconURL {
  59. _iconURL = iconURL;
  60. // iconURL = @"https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1536134884&di=9ea523019212b373ac04f4411e8aa56b&imgtype=jpg&er=1&src=http%3A%2F%2Fp3.music.126.net%2Fyp-UZgqWp6FV_AN4hPozjQ%3D%3D%2F109951163414711232.jpg";
  61. if (_iconURL.length > 0 && [_iconURL hasPrefix:@"http"]) {
  62. __weak typeof(self) weakSelf = self;
  63. [RASingleton.sharedInstance.networkQueue addOperationWithBlock:^{
  64. NSString *path = [UIImage imageCachePath:iconURL];
  65. UIImage *icon = nil;
  66. if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
  67. // 加载缓存的缩放后的图片
  68. icon = [UIImage imageWithContentsOfFile:path];
  69. } else {
  70. // 从服务器获取图片
  71. icon = [UIImage ra_imageWithURL:[NSURL URLWithString:iconURL]];
  72. // 缩放图片
  73. icon = [self antiAlias:icon];
  74. // 缓存缩放后的图片
  75. [self cacheScaledIcon:icon withPath:path];
  76. }
  77. if (weakSelf) {
  78. __strong typeof(weakSelf) strongSelf = weakSelf;
  79. [strongSelf setIcon:icon];
  80. }
  81. }];
  82. }
  83. }
  84. - (UIImage *)antiAlias:(UIImage *)img {
  85. CGFloat size = 50.0f;
  86. UIGraphicsBeginImageContextWithOptions(CGSizeMake(size, size), NO, [UIScreen mainScreen].scale);
  87. [img drawInRect:CGRectMake(0, 0, size, size)];
  88. UIImage *antialias = UIGraphicsGetImageFromCurrentImageContext();
  89. UIGraphicsEndImageContext();
  90. return antialias;
  91. }
  92. - (void)cacheScaledIcon:(UIImage *)icon withPath:(NSString *)path {
  93. NSData *data = [NSData dataWithContentsOfFile:path];
  94. RAIMAGETYPE type = [data imageType];
  95. if (type == RAIMAGETYPEPNG) {
  96. data = UIImagePNGRepresentation(icon);
  97. } else if (type == RAIMAGETYPEJPG) {
  98. data = UIImageJPEGRepresentation(icon, 1);
  99. } else {
  100. data = nil;
  101. }
  102. if (data) {
  103. [self cacheData:data toPath:path];
  104. }
  105. }
  106. - (void)cacheData:(NSData *)data toPath:(NSString *)path {
  107. [data writeToFile:path atomically:NO];
  108. }
  109. @end