فهرست منبع

fix out of memory when render image

kevin 2 سال پیش
والد
کامیت
59c2e4f685

+ 41 - 0
appbuilder-ios/NexilisLite/NexilisLite/Source/Extension.swift

@@ -1255,3 +1255,44 @@ extension String {
         lhs = lhs + rhs
     }
 }
+
+extension UIGraphicsRenderer {
+    static func renderImagesAt(urls: [NSURL], size: CGSize, scale: CGFloat = 1) -> UIImage {
+        let renderer = UIGraphicsImageRenderer(size: size)
+
+        let options: [NSString: Any] = [
+            kCGImageSourceThumbnailMaxPixelSize: max(size.width * scale, size.height * scale),
+            kCGImageSourceCreateThumbnailFromImageAlways: true
+        ]
+
+        let thumbnails = try urls.map { url -> CGImage in
+            let imageSource = CGImageSourceCreateWithURL(url, nil)
+
+            let scaledImage = CGImageSourceCreateThumbnailAtIndex(imageSource!, 0, options as CFDictionary)
+            
+            return scaledImage!
+        }
+
+        // Translate Y-axis down because cg images are flipped and it falls out of the frame (see bellow)
+        let rect = CGRect(x: 0,
+                          y: -size.height,
+                          width: size.width,
+                          height: size.height)
+
+        let resizedImage = renderer.image { ctx in
+
+            let context = ctx.cgContext
+            context.scaleBy(x: 1, y: -1) //Flip it ( cg y-axis is flipped)
+
+            for image in thumbnails {
+                context.draw(image, in: rect)
+            }
+        }
+
+        return resizedImage
+    }
+    
+    static func renderImageAt(url: NSURL, size: CGSize, scale: CGFloat = 1) -> UIImage {
+            return renderImagesAt(urls: [url], size: size, scale: scale)
+        }
+}

+ 9 - 3
appbuilder-ios/NexilisLite/NexilisLite/Source/View/Chat/EditorGroup.swift

@@ -99,6 +99,9 @@ public class EditorGroup: UIViewController {
             UserDefaults.standard.removeObject(forKey: "inEditorGroup")
             NotificationCenter.default.removeObserver(self)
         }
+        super.viewDidDisappear(true)
+        self.removeFromParent()
+        self.dismiss(animated: true, completion: nil)
     }
     
     public override func viewDidAppear(_ animated: Bool) {
@@ -3463,7 +3466,8 @@ 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(contentsOfFile: thumbURL.path)
+                let image = UIGraphicsRenderer.renderImageAt(url: thumbURL as NSURL, size: CGSize(width: 250, height: 250))
                 imageThumb.image = image
                 
                 let videoURL = URL(fileURLWithPath: dirPath).appendingPathComponent(videoChat)
@@ -3885,7 +3889,8 @@ 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(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
@@ -4460,7 +4465,8 @@ 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(contentsOfFile: thumbURL.path)
+                let image = UIGraphicsRenderer.renderImageAt(url: thumbURL as NSURL, size: CGSize(width: 250, height: 250))
                 let imageThumb = UIImageView(image: image)
                 self.containerPreviewReply.addSubview(imageThumb)
                 imageThumb.layer.cornerRadius = 2.0

+ 9 - 3
appbuilder-ios/NexilisLite/NexilisLite/Source/View/Chat/EditorPersonal.swift

@@ -116,6 +116,9 @@ public class EditorPersonal: UIViewController, ImageVideoPickerDelegate, UIGestu
             UserDefaults.standard.removeObject(forKey: "inEditorPersonal")
             NotificationCenter.default.removeObserver(self)
         }
+        super.viewDidDisappear(true)
+        self.removeFromParent()
+        self.dismiss(animated: true, completion: nil)
     }
     
     public override func viewDidAppear(_ animated: Bool) {
@@ -4786,7 +4789,8 @@ 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)
+//                let image    = UIImage(contentsOfFile: thumbURL.path)
+                let image = UIGraphicsRenderer.renderImageAt(url: thumbURL as NSURL, size: CGSize(width: 250, height: 250))
                 imageThumb.image = image
                 
                 let videoURL = URL(fileURLWithPath: dirPath).appendingPathComponent(videoChat)
@@ -5200,7 +5204,8 @@ 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(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
@@ -5834,7 +5839,8 @@ 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(contentsOfFile: thumbURL.path)
+                let image = UIGraphicsRenderer.renderImageAt(url: thumbURL as NSURL, size: CGSize(width: 250, height: 250))
                 let imageThumb = UIImageView(image: image)
                 self.containerPreviewReply.addSubview(imageThumb)
                 imageThumb.layer.cornerRadius = 2.0