// // RAHomeOrderModel.m // Apex And Drivers // // Created by Jack on 2018/6/1. // Copyright © 2018年 USAI. All rights reserved. // #import "RAHomeOrderModel.h" #import "UIImage+RedAnt.h" #import "NSData+RAImageType.h" @implementation RAHomeOrderModel { UIImage *_icon; UIColor *_orderTypeColor; } - (void)setValue:(id)value forUndefinedKey:(NSString *)key { } - (UIImage *)icon { return _icon; } - (UIColor *)orderTypeColor { if (!_orderTypeColor) { _orderTypeColor = [UIColor blackColor]; } return _orderTypeColor; } - (void)setColor:(NSString *)color { _color = color; if ([color hasPrefix:@"#"]) { color = [color stringByReplacingOccurrencesOfString:@"#" withString:@"0x"]; } if (![color hasPrefix:@"0x"]) { _orderTypeColor = nil; } _orderTypeColor = UIColorFromRGB(strtoul([color UTF8String], 0, 16)); } - (void)setBackendFlag:(BOOL)backendFlag { _backendFlag = backendFlag; if (self.delegate && [self.delegate respondsToSelector:@selector(refreshUI)]) { dispatch_async(dispatch_get_main_queue(), ^{ [self.delegate refreshUI]; }); } } - (void)setIcon:(UIImage *)icon { _icon = icon; // _icon = [UIImage imageNamed:@"on_the_way"]; if (self.delegate && [self.delegate respondsToSelector:@selector(refreshUI)]) { dispatch_async(dispatch_get_main_queue(), ^{ [self.delegate refreshUI]; }); } } //- (void)setValuesForKeysWithDictionary:(NSDictionary *)keyedValues { // [super setValuesForKeysWithDictionary:keyedValues]; // // [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"]; //} - (void)setIconURL:(NSString *)iconURL { _iconURL = iconURL; // 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"; if (_iconURL.length > 0 && [_iconURL hasPrefix:@"http"]) { __weak typeof(self) weakSelf = self; [RASingleton.sharedInstance.networkQueue addOperationWithBlock:^{ NSString *path = [UIImage imageCachePath:iconURL]; UIImage *icon = nil; if ([[NSFileManager defaultManager] fileExistsAtPath:path]) { // 加载缓存的缩放后的图片 icon = [UIImage imageWithContentsOfFile:path]; } else { // 从服务器获取图片 icon = [UIImage ra_imageWithURL:[NSURL URLWithString:iconURL]]; // 缩放图片 icon = [self antiAlias:icon]; // 缓存缩放后的图片 [self cacheScaledIcon:icon withPath:path]; } if (weakSelf) { __strong typeof(weakSelf) strongSelf = weakSelf; [strongSelf setIcon:icon]; } }]; } } - (UIImage *)antiAlias:(UIImage *)img { CGFloat size = 50.0f; UIGraphicsBeginImageContextWithOptions(CGSizeMake(size, size), NO, [UIScreen mainScreen].scale); [img drawInRect:CGRectMake(0, 0, size, size)]; UIImage *antialias = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return antialias; } - (void)cacheScaledIcon:(UIImage *)icon withPath:(NSString *)path { NSData *data = [NSData dataWithContentsOfFile:path]; RAIMAGETYPE type = [data imageType]; if (type == RAIMAGETYPEPNG) { data = UIImagePNGRepresentation(icon); } else if (type == RAIMAGETYPEJPG) { data = UIImageJPEGRepresentation(icon, 1); } else { data = nil; } if (data) { [self cacheData:data toPath:path]; } } - (void)cacheData:(NSData *)data toPath:(NSString *)path { [data writeToFile:path atomically:NO]; } @end