Actually I had an image file and text file. I wanted to compress the two files and add as an attachment to swift mail composer. Any Solution? Please help
Asked
Active
Viewed 839 times
1
-
For compressing image open this link. https://stackoverflow.com/questions/18681990/compress-images-to-reduce-file-size – Naresh Jul 14 '18 at 05:21
-
@iOS This is not the solution required for me. Thanks for the response – Rakesh Jul 14 '18 at 05:30
1 Answers
0
You can zip your files using this
https://github.com/marmelroy/Zip library
here is sample code added
do {
let filePath = Bundle.main.url(forResource: "file", withExtension: "zip")!
let documentsDirectory = FileManager.default.urls(for:.documentDirectory, in: .userDomainMask)[0]
try Zip.unzipFile(filePath, destination: documentsDirectory, overwrite: true, password: "password", progress: { (progress) -> () in
print(progress)
}) // Unzip
let zipFilePath = documentsFolder.appendingPathComponent("archive.zip")
try Zip.zipFiles([filePath], zipFilePath: zipFilePath, password: "password", progress: { (progress) -> () in
print(progress)
}) //Zip
}
catch {
print("Something went wrong")
}
And attach that zip to your mail composer
Hope it is helpful

Prashant Tukadiya
- 15,838
- 4
- 62
- 98
-
The solution is fine if we are fetching the files from a folder right. but I have files in the project itself and an image too. How can it be compressed? Please help – Rakesh Jul 14 '18 at 06:16
-
@Rakesh You can easily do it. you just need to save the files in your document directory for temporary time and you done with zip you can remove from that location. – Prashant Tukadiya Jul 14 '18 at 06:18
-
It is better to use filesystem as it will reduce load on your memory so there is least chance to memory issues in future – Prashant Tukadiya Jul 14 '18 at 06:22
-
This pod is only swift 4. Does it work in swift 3? As mentioned in the document it works with swift 4 – Rakesh Jul 14 '18 at 06:44
-
@Rakesh I am not sure about it what version of xcode you are using ? – Prashant Tukadiya Jul 14 '18 at 06:48