Browse Source

improvement code downloadContents(ofUrl

marinofaggiana 5 years ago
parent
commit
40159b308e

+ 2 - 8
iOSClient/Activity/NCActivity.swift

@@ -197,22 +197,16 @@ extension NCActivity: UITableViewDataSource {
                 let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + fileNameIcon
                 
                 if FileManager.default.fileExists(atPath: fileNameLocalPath) {
-                    
-                    if let image = UIImage(contentsOfFile: fileNameLocalPath) {
-                        cell.icon.image = image
-                    }
-                    
+                    if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.icon.image = image }
                 } else {
-                    
                     DispatchQueue.global().async {
-                        
                         let encodedString = activity.icon.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
                         OCNetworking.sharedManager()?.downloadContents(ofUrl: encodedString, completion: { (data, message, errorCode) in
                             if errorCode == 0 {
                                 do {
                                     try data!.write(to: NSURL(fileURLWithPath: fileNameLocalPath) as URL, options: .atomic)
+                                    if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.icon.image = image }
                                 } catch { return }
-                                cell.icon.image = UIImage(data: data!)
                             }
                         })
                     }

+ 2 - 1
iOSClient/Main/NCMainCommon.swift

@@ -647,7 +647,8 @@ class NCMainCommon: NSObject, PhotoEditorDelegate, NCAudioRecorderViewController
                         if errorCode == 0 && UIImage(data: data!) != nil {
                             do {
                                 try data!.write(to: NSURL(fileURLWithPath: fileNameLocalPath) as URL, options: .atomic)
-                            } catch {  }
+                                cell.shared.image = NCUtility.sharedInstance.createAvatar(image: UIImage(contentsOfFile: fileNameLocalPath), size: 30)
+                            } catch { return }
                         }
                     })
                 }

+ 5 - 3
iOSClient/Notification/CCNotification.swift

@@ -117,10 +117,12 @@ class CCNotification: UITableViewController, CCNotificationCelllDelegate {
                                 if errorCode == 0 && UIImage(data: data!) != nil  {
                                     do {
                                         try data!.write(to: NSURL(fileURLWithPath: fileNameLocalPath) as URL, options: .atomic)
+                                        if let image = UIImage(contentsOfFile: fileNameLocalPath) {
+                                            cell.avatar.isHidden = false
+                                            cell.avatarLeadingMargin.constant = 50
+                                            cell.avatar.image = image
+                                        }
                                     } catch { return }
-                                    cell.avatar.isHidden = false
-                                    cell.avatarLeadingMargin.constant = 50
-                                    cell.avatar.image = UIImage(data: data!)
                                 }
                             })
                         }

+ 6 - 12
iOSClient/Share/NCShare.swift

@@ -79,9 +79,7 @@ class NCShare: UIViewController, UIGestureRecognizerDelegate, NCShareLinkCellDel
             
             let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + metadata!.ownerId + ".png"
             if FileManager.default.fileExists(atPath: fileNameLocalPath) {
-                if let image = UIImage(contentsOfFile: fileNameLocalPath) {
-                    sharedWithYouByImage.image = image
-                }
+                if let image = UIImage(contentsOfFile: fileNameLocalPath) { sharedWithYouByImage.image = image }
             } else {
                 let url = appDelegate.activeUrl + k_avatar + metadata!.ownerId + "/128"
                 let encodedString = url.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
@@ -89,8 +87,8 @@ class NCShare: UIViewController, UIGestureRecognizerDelegate, NCShareLinkCellDel
                     if errorCode == 0 && UIImage(data: data!) != nil {
                         do {
                             try data!.write(to: NSURL(fileURLWithPath: fileNameLocalPath) as URL, options: .atomic)
+                            if let image = UIImage(contentsOfFile: fileNameLocalPath) { self.sharedWithYouByImage.image = image }
                         } catch { return }
-                        self.sharedWithYouByImage.image = UIImage(data: data!)
                     } else {
                         self.sharedWithYouByImage.image = UIImage(named: "avatar")
                     }
@@ -254,9 +252,7 @@ class NCShare: UIViewController, UIGestureRecognizerDelegate, NCShareLinkCellDel
 
             let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(self.appDelegate.activeUser, activeUrl: self.appDelegate.activeUrl) + "-" + item.name + ".png"
             if FileManager.default.fileExists(atPath: fileNameLocalPath) {
-                if let image = UIImage(contentsOfFile: fileNameLocalPath) {
-                    cell.imageItem.image = image
-                }
+                if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
             } else {
                 DispatchQueue.global().async {
                     let url = self.appDelegate.activeUrl + k_avatar + item.name + "/128"
@@ -265,8 +261,8 @@ class NCShare: UIViewController, UIGestureRecognizerDelegate, NCShareLinkCellDel
                         if errorCode == 0 && UIImage(data: data!) != nil {
                             do {
                                 try data!.write(to: NSURL(fileURLWithPath: fileNameLocalPath) as URL, options: .atomic)
+                                if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
                             } catch { return }
-                            cell.imageItem.image = UIImage(data: data!)
                         } else {
                             cell.imageItem.image = UIImage(named: "avatar")
                         }
@@ -346,9 +342,7 @@ extension NCShare: UITableViewDataSource {
                 
                 let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + tableShare.shareWith + ".png"
                 if FileManager.default.fileExists(atPath: fileNameLocalPath) {
-                    if let image = UIImage(contentsOfFile: fileNameLocalPath) {
-                        cell.imageItem.image = image
-                    }
+                    if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
                 } else {
                     DispatchQueue.global().async {
                         let url = self.appDelegate.activeUrl + k_avatar + tableShare.shareWith + "/128"
@@ -357,8 +351,8 @@ extension NCShare: UITableViewDataSource {
                             if errorCode == 0 && UIImage(data: data!) != nil {
                                 do {
                                     try data!.write(to: NSURL(fileURLWithPath: fileNameLocalPath) as URL, options: .atomic)
+                                    if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
                                 } catch { return }
-                                cell.imageItem.image = UIImage(data: data!)
                             } else {
                                 cell.imageItem.image = UIImage(named: "avatar")
                             }

+ 2 - 4
iOSClient/Share/NCShareComments.swift

@@ -218,9 +218,7 @@ extension NCShareComments: UITableViewDataSource {
             // Image
             let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + tableComments.actorId + ".png"
             if FileManager.default.fileExists(atPath: fileNameLocalPath) {
-                if let image = UIImage(contentsOfFile: fileNameLocalPath) {
-                    cell.imageItem.image = image
-                }
+                if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
             } else {
                 DispatchQueue.global().async {
                     let url = self.appDelegate.activeUrl + k_avatar + tableComments.actorId + "/128"
@@ -229,8 +227,8 @@ extension NCShareComments: UITableViewDataSource {
                         if errorCode == 0 && UIImage(data: data!) != nil {
                             do {
                                 try data!.write(to: NSURL(fileURLWithPath: fileNameLocalPath) as URL, options: .atomic)
+                                if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
                             } catch { return }
-                            cell.imageItem.image = UIImage(data: data!)
                         } else {
                             cell.imageItem.image = UIImage(named: "avatar")
                         }