我有一个UIView
,其中textfield
和图像将加载到UIImage
中,我保存了UIView
区域中包含的所有内容,但是保留了低分辨率,并且需要特定的尺寸。
@IBAction func shareButton(_ sender: AnyObject) {
renderImageWithText()
}
// RENDER IMAGE WITH TEXT
func renderImageWithText() {
let rect = cropView.bounds
UIGraphicsBeginImageContext(CGSize(width: cropView.frame.size.width, height: cropView.frame.size.height))
cropView.drawHierarchy(in: CGRect(x: 0.0, y: 0.0, width: cropView.frame.size.width, height: cropView.frame.size.height), afterScreenUpdates: true)
imageToBeShared = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
openShareMenu()
}
// OPEN SHARE
func openShareMenu() {
let shareItems = [imageToBeShared!] as [Any]
let actVC = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
actVC.excludedActivityTypes = [.print, .postToWeibo, .copyToPasteboard, .addToReadingList, .postToVimeo]
if UIDevice.current.userInterfaceIdiom == .pad {
// iPad
let popOver = UIPopoverController(contentViewController: actVC)
popOver.present(from: CGRect(x: self.view.frame.size.width/2, y: self.view.frame.size.height/2, width: 0, height: 0), in: self.view, permittedArrowDirections: .down, animated: true)
} else {
// iPhone
present(actVC, animated: true, completion: nil)
}
}