CNContactVCardSerialization+Helpers.swift 3.49 KB
Newer Older
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
/*
 *  Copyright (C) 2017 Savoir-faire Linux Inc.
 *
 *  Author: Kateryna Kostiuk <kateryna.kostiuk@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */
import Foundation
import Contacts
/*
 *This extension adds fields UID and PHOTO to vCard  provided by default
 *It also provides image compression that mostly could be useful when sending contact request
 */

enum VCardFields: String {
    case begin     = "BEGIN:VCARD"
    case uid       = "UID:"
    case photoJPEG = "PHOTO;TYPE=JPEG;ENCODING=BASE64:"
    case photoPNG  = "PHOTO;TYPE=PNG;ENCODING=BASE64:"
    case end       = "END:VCARD"
33 34
    case name      = "N:"
    case fullName  = "FN:"
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
35 36 37 38 39 40
}

extension CNContactVCardSerialization {

    class func dataWithImageAndUUID(from contact: CNContact, andImageCompression compressedSize: Int?) throws -> Data {

41 42 43 44 45 46 47
        // recreate vCard string
        let beginString = VCardFields.begin.rawValue + "\n"
        let entryUIDString = VCardFields.uid.rawValue + contact.identifier + "\n"
        let name = contact.familyName.trimmingCharacters(in: .whitespacesAndNewlines)
        let firstnameString = VCardFields.name.rawValue + name + "\n"
        let fullNameString = VCardFields.fullName.rawValue + name + "\n"
        let endString = VCardFields.end.rawValue
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
48

49
        var vCardString = beginString + entryUIDString + firstnameString + fullNameString + endString
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
50

51
        // if contact have an image add it to vCard data
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
52
        guard var image = contact.imageData  else {
53
            return vCardString.data(using: .utf8)!
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
54 55 56 57 58 59 60
        }

        var photofieldName = VCardFields.photoPNG

        // if we need smallest image first scale it and than compress
        var scaledImage: UIImage?
        if compressedSize != nil {
61 62
            scaledImage =  UIImage(data: image)?
                .convert(toSize: CGSize(width: 50.0, height: 50.0), scale: 1)
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
63 64 65 66 67 68 69 70 71 72
        }

        if let scaledImage = scaledImage {
            if UIImagePNGRepresentation(scaledImage) != nil {
                image = UIImagePNGRepresentation(scaledImage)!
            }
        }

        if let compressionSize = compressedSize, image.count > compressionSize {
            // compress image before sending vCard
73 74 75
            guard let compressedImage = UIImage(data: image)?
                .convertToData(ofMaxSize: compressionSize)else {
                    return vCardString.data(using: .utf8)!
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
76 77 78 79 80 81 82 83
            }

            image = compressedImage
            photofieldName = VCardFields.photoJPEG
        }

        let base64Image =  image.base64EncodedString(options: Data.Base64EncodingOptions.init(rawValue: 0))
        let vcardImageString = photofieldName.rawValue + base64Image + "\n"
84
        vCardString = vCardString.replacingOccurrences(of: VCardFields.end.rawValue, with: (vcardImageString + VCardFields.end.rawValue))
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
85

86
        return vCardString.data(using: .utf8)!
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
87 88
    }
}