WCY
WCY

Reputation: 33

Copy image to temp folder but failed to read

I have issue on copy image to a temp folder then read out from this. I can copy the image to the temporary folder ( with no error thrown). I trying to read out the item list in folder. The error i received is Error Domain=NSCocoaErrorDomain Code=256 "The file “21C6331F-609C-4719-ADCE-2B76B6C2EA3A” couldn’t be opened." UserInfo={NSUserStringVariant=( Folder ), NSFilePath=/private/var/mobile/Containers/Data/Application/*******/tmp/21C6331F-609C-4719-ADCE-2B76B6C2EA3A, NSUnderlyingError=0x282f0a070 {Error Domain=NSPOSIXErrorDomain Code=20 "Not a directory"}}

Here is my code:

func getNotificationImageUrl() -> URL? {
        guard let _documentPath = self.documentPath else{
            return nil
        }

        do {
            // Get the directory contents urls (including subfolders urls)
            let directoryContents = try FileManager.default.contentsOfDirectory( at:_documentPath, includingPropertiesForKeys: nil, options: [])
            // filter results get from folder
            let imageurl = directoryContents.first(where: { $0.lastPathComponent.contains("banner")})
            
            if let imageurl = imageurl {
                print("[DEBUG] [\(#function)] [\(#line)] imageurl : \(imageurl)")
                // create a temp directory
                let tempDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString, isDirectory: true)
                // copy item from directoryContents to temp folder
                try FileManager.default.copyItem(at: imageurl, to: tempDirectoryURL)

                // check whether copy item is successful 

                /************The throw here **************/
                let items = try FileManager.default.contentsOfDirectory(atPath: tempDirectoryURL.path)

                for item in items {
                    print("[DEBUG] [\(#function)] [\(#line)] Found \(item)")
                }
                
                return tempDirectoryURL
            }
            
        } catch let error as NSError {
            print("[DEBUG] [\(#function)] [\(#line)] getNotificationImageUrl : \(error) ")
        }

        return nil
    }

Example of directoryContents list :

[ file:///private/var/mobile/Containers/Data/Application/*******/Library/Application%20Support/Advertisement/a68j6fc15abcdc1/banner.png, file:///private/var/mobile/Containers/Data/Application/*******/Library/Application%20Support/Advertisement/a68j6fc15abcdc1/a68j6fc15abcdc1.zip, file:///private/var/mobile/Containers/Data/Application/*******/Library/Application%20Support/Advertisement/a68j6fc15abcdc1/a68j6fc15abcdc1.json, file:///private/var/mobile/Containers/Data/Application/*******/Library/Application%20Support/Advertisement/a68j6fc15abcdc1/launch.jpg ]

Thank you.

Hope this code and explanation can help you to get the picture. UNNotificationAttachment :

The system validates attachments before displaying the associated notification. If you attach a file to a local notification request that’s corrupted, invalid, or of an unsupported file type, the system doesn’t schedule your request. For remote notifications, the system validates attachments after your notification service app extension finishes. Once validated, the system moves the attached files into the attachment data store so that the appropriate processes can access the files. The system copies attachments located inside an app’s bundle.

After add attachment, it will be remove from my existing folder. Tried to convert from UIImage to JPEGData method before, it taking long time in background mode to process conversion and write data into file.

Upvotes: 0

Views: 203

Answers (0)

Related Questions