UIImageView content is not shared [duplicate]

£可爱£侵袭症+ 提交于 2019-12-13 09:18:16

问题


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?


回答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



来源:https://stackoverflow.com/questions/57526584/uiimageview-content-is-not-shared

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!