问题
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