| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- //
- // 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<NSString *,id> *)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
|