-1

I have this piece of code in my app

guard let codeImage = imgQRCode.image else {
        return
    }
    PHPhotoLibrary.requestAuthorization({status in
        DispatchQueue.main.async {
            if status == .authorized {
                let ac = UIActivityViewController(activityItems: [codeImage], applicationActivities: nil)
                self.present(ac, animated: true, completion: nil)
            } else {
                self.showOkAlert(messageTitle: "Access not granted", messageText: "Code image not saved.", okText: "OK", {})
            }
        }
})

But content displayed in imgQRCode UIImageView (generated QR Code) is not saved to Photo Library via UIActivityViewController and error "ContactQR[997:63907] [GatekeeperXPC] Connection to assetsd was interrupted or assetsd died" is shown in XCode console log. If I replace guard section with code

let codeImage = UIImage(named: "Ghost")

using picture from Assets it`s correctly shared to Photo Library. Why QR code image not if it's normal UIImage?

Dawy
  • 770
  • 6
  • 23

1 Answers1

-1

Finally it helped to render image of UIImageView

let renderer = UIGraphicsImageRenderer(size: imgQRCode.bounds.size)
let renderedImage = renderer.image(actions: {context in
    imgQRCode.drawHierarchy(in: imgQRCode.bounds, afterScreenUpdates: true)
})

so rest of the code

PHPhotoLibrary.requestAuthorization({status in
    DispatchQueue.main.async {
        if status == .authorized {
            let ac = UIActivityViewController(activityItems: [renderedImage], applicationActivities: nil)
            self.present(ac, animated: true, completion: nil)
        } else {
            self.showOkAlert(messageTitle: "Access not granted", messageText: "Code image not saved.", okText: "OK", {})
        }
    }
})

works perfectly now. Thanks @twostraws

Dawy
  • 770
  • 6
  • 23
  • This answer can in no way be derived from the information given in the question. It is also wrong, as you should not have to call `drawHierarchy` in order to make this work. The question says nothing about where the `image` comes from. Possibly you are using CIImage incorrectly (e.g. you tried to say `UIImage(ciImage:)` perhaps), but there is no way to tell, as you did not (and still have not) told us that information. – matt Aug 17 '19 at 15:46
  • Image comes from UIImage(ciImage: resampledQRCodeImage) – Dawy Aug 17 '19 at 16:31
  • Yup, well that’s the problem. That’s wrong. – matt Aug 17 '19 at 16:33