RAHomeOrderModel.m 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. }
  14. - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
  15. }
  16. - (UIImage *)icon {
  17. return _icon;
  18. }
  19. - (void)setBackendFlag:(BOOL)backendFlag {
  20. _backendFlag = backendFlag;
  21. if (self.delegate && [self.delegate respondsToSelector:@selector(refreshUI)]) {
  22. dispatch_async(dispatch_get_main_queue(), ^{
  23. [self.delegate refreshUI];
  24. });
  25. }
  26. }
  27. - (void)setIcon:(UIImage *)icon {
  28. _icon = icon;
  29. // _icon = [UIImage imageNamed:@"on_the_way"];
  30. if (self.delegate && [self.delegate respondsToSelector:@selector(refreshUI)]) {
  31. dispatch_async(dispatch_get_main_queue(), ^{
  32. [self.delegate refreshUI];
  33. });
  34. }
  35. }
  36. //- (void)setValuesForKeysWithDictionary:(NSDictionary<NSString *,id> *)keyedValues {
  37. // [super setValuesForKeysWithDictionary:keyedValues];
  38. //
  39. // [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"];
  40. //}
  41. - (void)setIconURL:(NSString *)iconURL {
  42. _iconURL = iconURL;
  43. // 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";
  44. if (_iconURL.length > 0 && [_iconURL hasPrefix:@"http"]) {
  45. __weak typeof(self) weakSelf = self;
  46. [RASingleton.sharedInstance.networkQueue addOperationWithBlock:^{
  47. NSString *path = [UIImage imageCachePath:iconURL];
  48. UIImage *icon = nil;
  49. if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
  50. // 加载缓存的缩放后的图片
  51. icon = [UIImage imageWithContentsOfFile:path];
  52. } else {
  53. // 从服务器获取图片
  54. icon = [UIImage ra_imageWithURL:[NSURL URLWithString:iconURL]];
  55. // 缩放图片
  56. icon = [self antiAlias:icon];
  57. // 缓存缩放后的图片
  58. [self cacheScaledIcon:icon withPath:path];
  59. }
  60. if (weakSelf) {
  61. __strong typeof(weakSelf) strongSelf = weakSelf;
  62. [strongSelf setIcon:icon];
  63. }
  64. }];
  65. }
  66. }
  67. - (UIImage *)antiAlias:(UIImage *)img {
  68. CGFloat size = 50.0f;
  69. UIGraphicsBeginImageContextWithOptions(CGSizeMake(size, size), NO, [UIScreen mainScreen].scale);
  70. [img drawInRect:CGRectMake(0, 0, size, size)];
  71. UIImage *antialias = UIGraphicsGetImageFromCurrentImageContext();
  72. UIGraphicsEndImageContext();
  73. return antialias;
  74. }
  75. - (void)cacheScaledIcon:(UIImage *)icon withPath:(NSString *)path {
  76. NSData *data = [NSData dataWithContentsOfFile:path];
  77. RAIMAGETYPE type = [data imageType];
  78. if (type == RAIMAGETYPEPNG) {
  79. data = UIImagePNGRepresentation(icon);
  80. } else if (type == RAIMAGETYPEJPG) {
  81. data = UIImageJPEGRepresentation(icon, 1);
  82. } else {
  83. data = nil;
  84. }
  85. if (data) {
  86. [self cacheData:data toPath:path];
  87. }
  88. }
  89. - (void)cacheData:(NSData *)data toPath:(NSString *)path {
  90. [data writeToFile:path atomically:NO];
  91. }
  92. @end