Why does UIImage from jpeg file and saved to png file not show as MSMessage in iMessage app?

When I get a UIImage from a jpeg file and then save it as a png file, it won't work with MSMessage. It's hard to tell what is going on because the print statements are not printing when the iMessage extension is run in debug mode in Xcode connected to an iPhone. I just noticed that it seems to have a problem when the UIImage came from a jpeg file.


The image is converted from jpeg to png and saved in iCloud by a UIDocument subclass in the containing app. In the iMessage extension, the image is retrieved from iCloud using the same UIDocument subclass and in an MSSticker in CollectionViewController. That part works fine. I think it's when the MSMessage object is created that I think the problem lies.


Here is my code:


UIDocument subclass:


    class ImageDocument: UIDocument {
       
        var image: UIImage? = nil
       
        override func load(fromContents contents: Any, ofType typeName: String?) throws {
           
            guard let data = contents as? Data else {
               
                throw DataFileError.fileReadFailed
               
            }
           
            image = UIImage(data: data)
           
        }
       
        override func contents(forType typeName: String) throws -> Any {
           
            guard image != nil else {
               
                throw DataFileError.badData
               
            }
           
            return image!.pngData() as Any
           
        }


    }



Image picker callback method in CollectionViewController:


    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
      
        print("!!! didFinishPickingMediaWithInfo")
      
        picker.dismiss(animated: true, completion: nil)
      
        var imageURL: URL?
        var image: UIImage?
      
        if #available(iOS 11.0, *) {
            imageURL = info[UIImagePickerController.InfoKey.imageURL] as? URL
        } else {
            image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage
        }
      
        let uuidString = UUID().uuidString
      
        var saveToURL: URL = ubiquityDocumentsURL!
      
        saveToURL.appendPathComponent(uuidString)
      
        saveToURL.appendPathExtension("png")
      
        print(saveToURL)
      
        if let imageURL = imageURL {
          
            if imageURL.pathExtension == saveToURL.pathExtension {
              
                print("!!!!! path extensions equal !!!!!")
              
                do {
                  
                    try FileManager.default.copyItem(at: imageURL, to: saveToURL)
                  
                    let document = ImageDocument(fileURL: saveToURL)
                  
                    document.open() {
                      
                        success in
                      
                        if success {
                          
                            let sticker = Sticker(rawValue: saveToURL.lastPathComponent, image: document.image!)
                          
                            self.items.append(sticker)
                          
                            DispatchQueue.main.async {
                              
                                self.collectionView.reloadData()
                              
                            }
                          
                        }
                      
                    } // document.open()


                } catch {
                  
                    print(error.localizedDescription)
                  
                }


            } else {
              
                print("!!!!! imageURL.pathExtension=", imageURL.pathExtension)
              
                let document = ImageDocument(fileURL: imageURL)
  
                document.open() {
                  
                    success in
                  
                    if success {
                      
                        document.save(to: saveToURL, for: UIDocument.SaveOperation.forCreating) {
                          
                            (success: Bool) in
                          
                            print("document.save success - ", success)
                          
                            print("document.fileURL=", document.fileURL)
                          
                            if success {
                              
                                let sticker = Sticker(rawValue: saveToURL.lastPathComponent, image: document.image!)
                              
                                self.items.append(sticker)
                              
                                DispatchQueue.main.async {
                                  
                                    self.collectionView.reloadData()
                                  
                                }
                              
                            }
                          
                        }
                      
                    }
                  
                }
              
            } // if imageURL.pathExtension == saveToURL.pathExtension ELSE
          
        } else if let image = image {
          
            print("!!!!! image=", image as Any)
          
            let document = ImageDocument(fileURL: saveToURL)
            document.image = image
          
            document.save(to: document.fileURL, for: UIDocument.SaveOperation.forCreating) {
              
                (success: Bool) in
              
                print("document.save success - ", success)
              
                print("document.fileURL=", document.fileURL)


                if success {
                  
                    let sticker = Sticker(rawValue: saveToURL.lastPathComponent, image: image)
                  
                    self.items.append(sticker)
                  
                    DispatchQueue.main.async {


                        self.collectionView.reloadData()
                      
                    }


                }


            }


        } else {
          
            fatalError("Error in ViewController - both mediaURL and image are nil!")
          
        }
      
    }


Collection view override in CollectionViewController:


    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
     
        let item = items[indexPath.row]
     
        print("item=", item)
     
        delegate?.selected(item)
     
    }



Event fired from CollectionViewController:


    extension MessagesViewController: CollectionViewControllerDelegate {
    
        func selected(_ sticker: Sticker) {
        
            print("sticker=", sticker)
        
            guard let conversation = activeConversation else { fatalError("Expected a conversation") }
        
            // Create a new message with the same session as any currently selected message.
            let message = composeMessage(with: sticker, caption: "messageCaption", session: conversation.selectedMessage?.session)
        
            /// - Tag: InsertMessageInConversation
            // Add the message to the conversation.
            conversation.insert(message) { error in
                if let error = error {
                    print(error)
                }
            }
        
        }
    
    }


Helper function:


    fileprivate func composeMessage(with sticker: Sticker, caption: String, session: MSSession? = nil) -> MSMessage {
   
        let layout = MSMessageTemplateLayout()
        layout.image = sticker.image
        layout.caption = caption
   
        print("layout.image=", layout.image as Any)
   
        let message = MSMessage(session: session ?? MSSession())
        message.layout = layout
   
        return message
   
    }



CollectionViewCell:


    class CollectionViewCell: UICollectionViewCell {
  
        static let reuseIdentifier = "CollectionViewCell"
  
        var representedSticker: Sticker!


        @IBOutlet weak var stickerView: MSStickerView!
  
    }



Sticker class is used as array "items" in CollectionViewController to be shown in collection view cells.


Sticker class:


    protocol StickerProtocol {

        var rawValue: String { get }

        var image: UIImage { get set }

    }


    struct Sticker: StickerProtocol {

        var rawValue: String

        var image: UIImage


    }