Procházet zdrojové kódy

add image cache in DigiX

alqindiirsyam před 2 roky
rodič
revize
c3bb32a123

+ 5 - 0
appbuilder-ios/DigiXLite/DigiXLite/Source/DigiX.swift

@@ -54,6 +54,8 @@ public class DigiX: NSObject {
     
     public static var loadingAlert = LibAlertController()
     
+    public static var imageCache = NSCache<NSString, UIImage>()
+    
     public static let listenerReceiveChat = "onReceiveChatLibLite"
     public static let listenerStatusChat = "onMessageChatLibLite"
     public static let listenerTypingChat = "onTypingChatLibLite"
@@ -99,6 +101,9 @@ public class DigiX: NSObject {
             
             IncomingThread.default.run()
             
+            imageCache.countLimit = 100
+            imageCache.totalCostLimit = 1024 * 1024 * 200
+            
             let address = DigiX.getAddress(apiKey:apiKey)
             if address.count == 0 {
                 return

+ 41 - 4
appbuilder-ios/DigiXLite/DigiXLite/Source/View/Chat/EditorGroup.swift

@@ -634,6 +634,7 @@ public class EditorGroup: UIViewController {
             searchBar.autocapitalizationType = .none
             searchBar.delegate = self
             searchBar.searchTextField.tintColor = .mainColor
+            searchBar.searchTextField.textColor = .mainColor
             searchBar.barTintColor = .secondaryColor
             searchBar.searchTextField.backgroundColor = .secondaryColor
             searchBar.showsCancelButton = false
@@ -3581,7 +3582,16 @@ extension EditorGroup: UITableViewDelegate, UITableViewDataSource {
                     let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
                     if let dirPath = paths.first {
                         let thumbURL = URL(fileURLWithPath: dirPath).appendingPathComponent(listImages[i].thumbId)
-                        let image    = UIImage(contentsOfFile: thumbURL.path)
+                        let image : UIImage? =  {
+                            if let img = DigiX.imageCache.object(forKey: listImages[i].thumbId as NSString) {
+                                return img
+                            }
+                            else if let img = UIImage(contentsOfFile: thumbURL.path)?.resize(target: CGSize(width: 500, height: 500)) {
+                                    DigiX.imageCache.setObject(img, forKey: listImages[i].thumbId as NSString)
+                                    return img
+                            }
+                            return nil
+                        }()
 //                        let image = UIGraphicsRenderer.renderImageAt(url: thumbURL as NSURL, size: CGSize(width: 250, height: 250))
                         listImageThumb[i].image = image
 
@@ -3685,7 +3695,16 @@ extension EditorGroup: UITableViewDelegate, UITableViewDataSource {
                 let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
                 if let dirPath = paths.first {
                     let thumbURL = URL(fileURLWithPath: dirPath).appendingPathComponent(thumbChat)
-                    let image    = UIImage(contentsOfFile: thumbURL.path)
+                    let image : UIImage? =  {
+                        if let img = DigiX.imageCache.object(forKey: thumbChat as NSString) {
+                            return img
+                        }
+                        else if let img = UIImage(contentsOfFile: thumbURL.path)?.resize(target: CGSize(width: 500, height: 500)) {
+                                DigiX.imageCache.setObject(img, forKey: thumbChat as NSString)
+                                return img
+                        }
+                        return nil
+                    }()
     //                let image = UIGraphicsRenderer.renderImageAt(url: thumbURL as NSURL, size: CGSize(width: 250, height: 250))
                     imageThumb.image = image
                     
@@ -4109,7 +4128,16 @@ extension EditorGroup: UITableViewDelegate, UITableViewDataSource {
                     let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
                     if let dirPath = paths.first {
                         let thumbURL = URL(fileURLWithPath: dirPath).appendingPathComponent(thumb_chat)
-                        let image    = UIImage(contentsOfFile: thumbURL.path)
+                        let image : UIImage? =  {
+                            if let img = DigiX.imageCache.object(forKey: thumb_chat as NSString) {
+                                return img
+                            }
+                            else if let img = UIImage(contentsOfFile: thumbURL.path)?.resize(target: CGSize(width: 500, height: 500)) {
+                                    DigiX.imageCache.setObject(img, forKey: thumb_chat as NSString)
+                                    return img
+                            }
+                            return nil
+                        }()
 //                        let image = UIGraphicsRenderer.renderImageAt(url: thumbURL as NSURL, size: CGSize(width: 250, height: 250))
                         let imageThumb = UIImageView(image: image)
                         containerReply.addSubview(imageThumb)
@@ -4778,7 +4806,16 @@ extension EditorGroup: UITableViewDelegate, UITableViewDataSource {
             let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
             if let dirPath = paths.first {
                 let thumbURL = URL(fileURLWithPath: dirPath).appendingPathComponent(thumb_chat)
-                let image    = UIImage(contentsOfFile: thumbURL.path)
+                let image : UIImage? =  {
+                    if let img = DigiX.imageCache.object(forKey: thumb_chat as NSString) {
+                        return img
+                    }
+                    else if let img = UIImage(contentsOfFile: thumbURL.path)?.resize(target: CGSize(width: 500, height: 500)) {
+                            DigiX.imageCache.setObject(img, forKey: thumb_chat as NSString)
+                            return img
+                    }
+                    return nil
+                }()
 //                let image = UIGraphicsRenderer.renderImageAt(url: thumbURL as NSURL, size: CGSize(width: 250, height: 250))
                 let imageThumb = UIImageView(image: image)
                 self.containerPreviewReply.addSubview(imageThumb)

+ 61 - 30
appbuilder-ios/DigiXLite/DigiXLite/Source/View/Chat/EditorPersonal.swift

@@ -823,6 +823,7 @@ public class EditorPersonal: UIViewController, ImageVideoPickerDelegate, UIGestu
             searchBar.autocapitalizationType = .none
             searchBar.delegate = self
             searchBar.searchTextField.tintColor = .mainColor
+            searchBar.searchTextField.textColor = .mainColor
             searchBar.barTintColor = .secondaryColor
             searchBar.searchTextField.backgroundColor = .secondaryColor
             searchBar.showsCancelButton = false
@@ -4973,7 +4974,7 @@ extension EditorPersonal: UITableViewDelegate, UITableViewDataSource {
                     let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
                     if let dirPath = paths.first {
                         let thumbURL = URL(fileURLWithPath: dirPath).appendingPathComponent(listImages[i].thumbId)
-                        let image    = UIImage(contentsOfFile: thumbURL.path)
+                        let image    = UIImage(contentsOfFile: thumbURL.path)?.resize(target: CGSize(width: 500, height: 500))
 //                        let image = UIGraphicsRenderer.renderImageAt(url: thumbURL as NSURL, size: CGSize(width: 250, height: 250))
                         listImageThumb[i].image = image
 
@@ -5077,10 +5078,20 @@ extension EditorPersonal: UITableViewDelegate, UITableViewDataSource {
                 let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
                 if let dirPath = paths.first {
                     let thumbURL = URL(fileURLWithPath: dirPath).appendingPathComponent(thumbChat)
-                    let image    = UIImage(contentsOfFile: thumbURL.path)
+                    DispatchQueue.main.async {
+                        let image : UIImage? =  {
+                            if let img = DigiX.imageCache.object(forKey: thumbChat as NSString) {
+                                return img
+                            }
+                            else if let img = UIImage(contentsOfFile: thumbURL.path)?.resize(target: CGSize(width: 500, height: 500)) {
+                                    DigiX.imageCache.setObject(img, forKey: thumbChat as NSString)
+                                    return img
+                            }
+                            return nil
+                        }()
+                        imageThumb.image = image
+                    }
 //                    let image = UIGraphicsRenderer.renderImageAt(url: thumbURL as NSURL, size: CGSize(width: 250, height: 250))
-                    imageThumb.image = image
-                    
                     let videoURL = URL(fileURLWithPath: dirPath).appendingPathComponent(videoChat)
                     let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent(imageChat)
                     if !FileManager.default.fileExists(atPath: imageURL.path) || !FileManager.default.fileExists(atPath: videoURL.path) {
@@ -5493,32 +5504,43 @@ extension EditorPersonal: UITableViewDelegate, UITableViewDataSource {
                     let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
                     if let dirPath = paths.first {
                         let thumbURL = URL(fileURLWithPath: dirPath).appendingPathComponent(thumb_chat)
-                        let image    = UIImage(contentsOfFile: thumbURL.path)
-//                        let image = UIGraphicsRenderer.renderImageAt(url: thumbURL as NSURL, size: CGSize(width: 250, height: 250))
-                        let imageThumb = UIImageView(image: image)
-                        containerReply.addSubview(imageThumb)
-                        imageThumb.layer.cornerRadius = 2.0
-                        imageThumb.clipsToBounds = true
-                        imageThumb.contentMode = .scaleAspectFill
-                        imageThumb.translatesAutoresizingMaskIntoConstraints = false
-                        imageThumb.trailingAnchor.constraint(equalTo: containerReply.trailingAnchor, constant: -10).isActive = true
-                        imageThumb.centerYAnchor.constraint(equalTo: containerReply.centerYAnchor).isActive = true
-                        imageThumb.widthAnchor.constraint(equalToConstant: 30).isActive = true
-                        imageThumb.heightAnchor.constraint(equalToConstant: 30).isActive = true
-                        
-                        if (attachment_flag == "2") {
-                            let imagePlay = UIImageView(image: UIImage(systemName: "play.circle.fill"))
-                            imageThumb.addSubview(imagePlay)
-                            imagePlay.clipsToBounds = true
-                            imagePlay.translatesAutoresizingMaskIntoConstraints = false
-                            imagePlay.centerYAnchor.constraint(equalTo: imageThumb.centerYAnchor).isActive = true
-                            imagePlay.centerXAnchor.constraint(equalTo: imageThumb.centerXAnchor).isActive = true
-                            imagePlay.widthAnchor.constraint(equalToConstant: 10).isActive = true
-                            imagePlay.heightAnchor.constraint(equalToConstant: 10).isActive = true
-                            imagePlay.tintColor = .white
+                        DispatchQueue.main.async {
+                            let image : UIImage? =  {
+                                if let img = DigiX.imageCache.object(forKey: thumbChat as NSString) {
+                                    return img
+                                }
+                                else if let img = UIImage(contentsOfFile: thumbURL.path)?.resize(target: CGSize(width: 500, height: 500)) {
+                                    DigiX.imageCache.setObject(img, forKey: thumbChat as NSString)
+                                    return img
+                                }
+                                return nil
+                            }()
+                            //                        let image = UIGraphicsRenderer.renderImageAt(url: thumbURL as NSURL, size: CGSize(width: 250, height: 250))
+                            let imageThumb = UIImageView(image: image)
+                            containerReply.addSubview(imageThumb)
+                            imageThumb.layer.cornerRadius = 2.0
+                            imageThumb.clipsToBounds = true
+                            imageThumb.contentMode = .scaleAspectFill
+                            imageThumb.translatesAutoresizingMaskIntoConstraints = false
+                            imageThumb.trailingAnchor.constraint(equalTo: containerReply.trailingAnchor, constant: -10).isActive = true
+                            imageThumb.centerYAnchor.constraint(equalTo: containerReply.centerYAnchor).isActive = true
+                            imageThumb.widthAnchor.constraint(equalToConstant: 30).isActive = true
+                            imageThumb.heightAnchor.constraint(equalToConstant: 30).isActive = true
+                            
+                            if (attachment_flag == "2") {
+                                let imagePlay = UIImageView(image: UIImage(systemName: "play.circle.fill"))
+                                imageThumb.addSubview(imagePlay)
+                                imagePlay.clipsToBounds = true
+                                imagePlay.translatesAutoresizingMaskIntoConstraints = false
+                                imagePlay.centerYAnchor.constraint(equalTo: imageThumb.centerYAnchor).isActive = true
+                                imagePlay.centerXAnchor.constraint(equalTo: imageThumb.centerXAnchor).isActive = true
+                                imagePlay.widthAnchor.constraint(equalToConstant: 10).isActive = true
+                                imagePlay.heightAnchor.constraint(equalToConstant: 10).isActive = true
+                                imagePlay.tintColor = .white
+                            }
+                            titleReply.trailingAnchor.constraint(equalTo: imageThumb.leadingAnchor, constant: -20).isActive = true
+                            contentReply.trailingAnchor.constraint(equalTo: imageThumb.leadingAnchor, constant: -20).isActive = true
                         }
-                        titleReply.trailingAnchor.constraint(equalTo: imageThumb.leadingAnchor, constant: -20).isActive = true
-                        contentReply.trailingAnchor.constraint(equalTo: imageThumb.leadingAnchor, constant: -20).isActive = true
                     }
                 }
                 if (attachment_flag == "11" && message_text.components(separatedBy: "/").count > 1) {
@@ -6219,7 +6241,16 @@ extension EditorPersonal: UITableViewDelegate, UITableViewDataSource {
             let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
             if let dirPath = paths.first {
                 let thumbURL = URL(fileURLWithPath: dirPath).appendingPathComponent(thumb_chat)
-                let image    = UIImage(contentsOfFile: thumbURL.path)
+                let image : UIImage? =  {
+                    if let img = DigiX.imageCache.object(forKey: thumb_chat as NSString) {
+                        return img
+                    }
+                    else if let img = UIImage(contentsOfFile: thumbURL.path)?.resize(target: CGSize(width: 500, height: 500)) {
+                            DigiX.imageCache.setObject(img, forKey: thumb_chat as NSString)
+                            return img
+                    }
+                    return nil
+                }()
 //                let image = UIGraphicsRenderer.renderImageAt(url: thumbURL as NSURL, size: CGSize(width: 250, height: 250))
                 let imageThumb = UIImageView(image: image)
                 self.containerPreviewReply.addSubview(imageThumb)

+ 30 - 8
appbuilder-ios/DigiXLite/DigiXLite/Source/View/Chat/ListGroupImages.swift

@@ -137,14 +137,36 @@ class ListGroupImages: UIViewController, UITableViewDataSource, UITableViewDeleg
         let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
         let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
         if let dirPath = paths.first {
-            let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent(listGroupingImages[indexPath.row].imageId)
-            let image    = UIImage(contentsOfFile: imageURL.path)
-            if image == nil {
-                let thumbURL = URL(fileURLWithPath: dirPath).appendingPathComponent(listGroupingImages[indexPath.row].thumbId)
-                let image    = UIImage(contentsOfFile: thumbURL.path)
-                containerImages.image = image
-            } else {
-                containerImages.image = image
+            let imageId = listGroupingImages[indexPath.row].imageId
+            let thumbId = listGroupingImages[indexPath.row].thumbId
+            let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent(imageId)
+            DispatchQueue.main.async {
+                let image : UIImage? =  {
+                    if let img = DigiX.imageCache.object(forKey: imageId as NSString) {
+                        return img
+                    }
+                    else if let img = UIImage(contentsOfFile: imageURL.path)?.resize(target: CGSize(width: 1000, height: 1000)) {
+                            DigiX.imageCache.setObject(img, forKey: imageId as NSString)
+                            return img
+                    }
+                    return nil
+                }()
+                if image == nil {
+                    let thumbURL = URL(fileURLWithPath: dirPath).appendingPathComponent(self.listGroupingImages[indexPath.row].thumbId)
+                    let image : UIImage? =  {
+                        if let img = DigiX.imageCache.object(forKey: thumbId as NSString) {
+                            return img
+                        }
+                        else if let img = UIImage(contentsOfFile: thumbURL.path)?.resize(target: CGSize(width: 500, height: 500)) {
+                                DigiX.imageCache.setObject(img, forKey: thumbId as NSString)
+                                return img
+                        }
+                        return nil
+                    }()
+                    containerImages.image = image
+                } else {
+                    containerImages.image = image
+                }
             }
             if !FileManager.default.fileExists(atPath: imageURL.path) {
                 let blurEffect = UIBlurEffect(style: UIBlurEffect.Style.light)

+ 10 - 1
appbuilder-ios/NexilisLite/NexilisLite/Source/View/Chat/EditorPersonal.swift

@@ -4974,7 +4974,16 @@ extension EditorPersonal: UITableViewDelegate, UITableViewDataSource {
                     let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
                     if let dirPath = paths.first {
                         let thumbURL = URL(fileURLWithPath: dirPath).appendingPathComponent(listImages[i].thumbId)
-                        let image    = UIImage(contentsOfFile: thumbURL.path)?.resize(target: CGSize(width: 500, height: 500))
+                        let image : UIImage? =  {
+                            if let img = Nexilis.imageCache.object(forKey: listImages[i].thumbId as NSString) {
+                                return img
+                            }
+                            else if let img = UIImage(contentsOfFile: thumbURL.path)?.resize(target: CGSize(width: 500, height: 500)) {
+                                Nexilis.imageCache.setObject(img, forKey: listImages[i].thumbId as NSString)
+                                return img
+                            }
+                            return nil
+                        }()
 //                        let image = UIGraphicsRenderer.renderImageAt(url: thumbURL as NSURL, size: CGSize(width: 250, height: 250))
                         listImageThumb[i].image = image