Răsfoiți Sursa

1.修改iOS Apex Driver图像缩放与缓存。

Pen Li 7 ani în urmă
părinte
comite
7b73fe79ee

+ 6 - 0
Redant Drivers/Apex And Drivers.xcodeproj/project.pbxproj

@@ -37,6 +37,7 @@
 		420D112B2133F98600149B37 /* fake_order_filter.json in Resources */ = {isa = PBXBuildFile; fileRef = 420D112A2133F98600149B37 /* fake_order_filter.json */; };
 		421C417621719EB500835447 /* RALoginNavigationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 421C417521719EB500835447 /* RALoginNavigationController.m */; };
 		421C417921719F8100835447 /* RALoginBackgroundView.m in Sources */ = {isa = PBXBuildFile; fileRef = 421C417821719F8100835447 /* RALoginBackgroundView.m */; };
+		422B06BC21743ABE003DA2DA /* NSData+RAImageType.m in Sources */ = {isa = PBXBuildFile; fileRef = 422B06BB21743ABE003DA2DA /* NSData+RAImageType.m */; };
 		422BD95C213CE0F300DF8E89 /* HomeHeader.xib in Resources */ = {isa = PBXBuildFile; fileRef = 422BD95B213CE0F300DF8E89 /* HomeHeader.xib */; };
 		422BD95F213CE2E600DF8E89 /* RABadgeNumberView.m in Sources */ = {isa = PBXBuildFile; fileRef = 422BD95E213CE2E600DF8E89 /* RABadgeNumberView.m */; };
 		422DA1C02134FA7D0021BD70 /* RAPresentationController.m in Sources */ = {isa = PBXBuildFile; fileRef = 422DA1BF2134FA7D0021BD70 /* RAPresentationController.m */; };
@@ -261,6 +262,8 @@
 		421C417521719EB500835447 /* RALoginNavigationController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RALoginNavigationController.m; sourceTree = "<group>"; };
 		421C417721719F8100835447 /* RALoginBackgroundView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RALoginBackgroundView.h; sourceTree = "<group>"; };
 		421C417821719F8100835447 /* RALoginBackgroundView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RALoginBackgroundView.m; sourceTree = "<group>"; };
+		422B06BA21743ABE003DA2DA /* NSData+RAImageType.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "NSData+RAImageType.h"; sourceTree = "<group>"; };
+		422B06BB21743ABE003DA2DA /* NSData+RAImageType.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "NSData+RAImageType.m"; sourceTree = "<group>"; };
 		422BD95B213CE0F300DF8E89 /* HomeHeader.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = HomeHeader.xib; sourceTree = "<group>"; };
 		422BD95D213CE2E600DF8E89 /* RABadgeNumberView.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RABadgeNumberView.h; sourceTree = "<group>"; };
 		422BD95E213CE2E600DF8E89 /* RABadgeNumberView.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RABadgeNumberView.m; sourceTree = "<group>"; };
@@ -787,6 +790,8 @@
 				425B97E320C7BD3800B35713 /* UIView+Toast.m */,
 				424BA1E12136806C007912EC /* UIImage+RedAnt.h */,
 				424BA1E22136806C007912EC /* UIImage+RedAnt.m */,
+				422B06BA21743ABE003DA2DA /* NSData+RAImageType.h */,
+				422B06BB21743ABE003DA2DA /* NSData+RAImageType.m */,
 				422BD95D213CE2E600DF8E89 /* RABadgeNumberView.h */,
 				422BD95E213CE2E600DF8E89 /* RABadgeNumberView.m */,
 			);
@@ -1513,6 +1518,7 @@
 				4205FD7220C1465400DB42B4 /* RAHomeOrderModel.m in Sources */,
 				4255EC6B20C4E28400E5DA24 /* RADetailActionSelectionViewController+TableDelegate.m in Sources */,
 				4235FAAD213E255D000B6672 /* SignatureViewController.m in Sources */,
+				422B06BC21743ABE003DA2DA /* NSData+RAImageType.m in Sources */,
 				420D110F2133EEE300149B37 /* RAOrderFilterInputModel.m in Sources */,
 				421C417621719EB500835447 /* RALoginNavigationController.m in Sources */,
 				42529D3C20C0EA3F000C0F4D /* zip.c in Sources */,

+ 41 - 2
Redant Drivers/Apex And Drivers/Home/RAHomeOrderModel.m

@@ -8,6 +8,7 @@
 
 #import "RAHomeOrderModel.h"
 #import "UIImage+RedAnt.h"
+#import "NSData+RAImageType.h"
 
 @implementation RAHomeOrderModel {
     UIImage *_icon;
@@ -49,12 +50,28 @@
 
 - (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:^{
             
-            UIImage *icon = [UIImage ra_imageWithURL:[NSURL URLWithString:iconURL]];
-            icon = [self antiAlias:icon];
+            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];
@@ -75,4 +92,26 @@
     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

+ 24 - 0
Redant Drivers/Apex And Drivers/NSData+RAImageType.h

@@ -0,0 +1,24 @@
+//
+//  NSData+RAImageType.h
+//  Apex And Drivers
+//
+//  Created by Jack on 2018/10/15.
+//  Copyright © 2018年 USAI. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+typedef NS_ENUM(NSInteger,RAIMAGETYPE) {
+    RAIMAGETYPEPNG,
+    RAIMAGETYPEJPG,
+    RAIMAGETYPEBMP,
+    RAIMAGETYPEGIF,
+    RAIMAGETYPEUNKNOW
+};
+
+@interface NSData (RAImageType)
+
+-(RAIMAGETYPE)imageType;
+
+@end
+

+ 43 - 0
Redant Drivers/Apex And Drivers/NSData+RAImageType.m

@@ -0,0 +1,43 @@
+//
+//  NSData+RAImageType.m
+//  Apex And Drivers
+//
+//  Created by Jack on 2018/10/15.
+//  Copyright © 2018年 USAI. All rights reserved.
+//
+
+#import "NSData+RAImageType.h"
+
+@implementation NSData (RAImageType)
+
+-(RAIMAGETYPE)imageType {
+    if (self.length > 4) {
+        const unsigned char *byte = self.bytes;
+        // jpeg/jpg
+        // ff d8
+        if (byte[0] == 0xff && byte[1] == 0xd8 && byte[2] == 0xff) {
+            return RAIMAGETYPEJPG;
+        }
+        // png
+        // 89 50 4e 47
+        if (byte[0] == 0x89 && byte[1] == 0x50 && byte[2] == 0x4e && byte[3] == 0x47) {
+            return RAIMAGETYPEPNG;
+        }
+        // bmp
+        // 42 4D
+        if (byte[0] == 0x42 && byte[1] == 0x4d) {
+            return RAIMAGETYPEBMP;
+        }
+        
+        // gif
+        // GIF比对[47][49][46]与第五个字符39(37)
+        if (byte[0] == 0x47 && byte[1] == 0x49 && byte[2] == 0x46 && (byte[4] == 0x39 || byte[4] == 0x37)) {
+            return RAIMAGETYPEGIF;
+        }
+        
+        return RAIMAGETYPEUNKNOW;
+    }
+    return RAIMAGETYPEUNKNOW;
+}
+
+@end

+ 4 - 0
Redant Drivers/Apex And Drivers/UIImage+RedAnt.h

@@ -12,6 +12,10 @@
 
 + (instancetype)ra_imageWithURL:(NSURL *)url;
 
++ (NSString *)imageCacheDir;
+
++ (NSString *)imageCachePath:(NSString *)url;
+
 + (UIImage *)img_compress:(UIImage*)image kbsize:(float) size;
 
 + (UIImage *)scaleImageToSize:(UIImage *)img size:(CGSize)size;

+ 13 - 1
Redant Drivers/Apex And Drivers/UIImage+RedAnt.m

@@ -53,7 +53,9 @@ static dispatch_semaphore_t _lock;
         
         if (imgData) {
             Lock();
-            [imgData writeToFile:imgPath atomically:NO];
+            if (![fileManager fileExistsAtPath:imgPath]) {
+                [imgData writeToFile:imgPath atomically:NO];
+            }
             Unlock();
             
             return [UIImage imageWithData:imgData];
@@ -67,6 +69,16 @@ static dispatch_semaphore_t _lock;
     return [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"ImageCache"];
 }
 
++ (NSString *)imageCachePath:(NSString *)url {
+    
+    NSString *md5 = [self md5:url];
+    
+    NSString *imgDir = [self imageCacheDir];
+    NSString *imgPath = [imgDir stringByAppendingPathComponent:md5];
+    
+    return imgPath;
+}
+
 + (nullable NSString *)md5:(nullable NSString *)str {
     if (!str) return nil;