swift2 在Swift中进行截图并在顶部添加文本/水印

owfi6suc  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(250)

我试图让用户能够分享一个应用程序的截图并分享它。我已经得到了一个截图和分享,但有兴趣找出是否有人知道如何添加一层文字上的截图时,正在采取的,有点像一个水印。

我在这里截图:

let layer = UIApplication.sharedApplication().keyWindow!.layer
        let scale = UIScreen.mainScreen().scale
        UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);

        layer.renderInContext(UIGraphicsGetCurrentContext()!)
        let screenshot = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        let croppedImage = self.cropImage(screenshot)
        let activityViewController = UIActivityViewController(activityItems: [croppedImage], applicationActivities: nil)
        self.presentViewController(activityViewController, animated: true, completion: nil)

我在此功能中裁剪屏幕截图图像,使其仅显示屏幕中间:

func cropImage(screenshot: UIImage) -> UIImage {
    let scale = screenshot.scale
    let imgSize = screenshot.size
    let screenHeight = UIScreen.mainScreen().bounds.height
    let bound = self.view.bounds.height
    let navHeight = self.navigationController!.navigationBar.frame.height
    let bottomBarHeight = screenHeight - navHeight - bound
    let crop = CGRectMake(0, 200, //"start" at the upper-left corner
        (imgSize.width - 1) * scale, //include half the width of the whole screen
        (imgSize.height - bottomBarHeight - 300) * scale) //include the height of the navigationBar and the height of view

    let cgImage = CGImageCreateWithImageInRect(screenshot.CGImage, crop)
    let image: UIImage = UIImage(CGImage: cgImage!)
    return image
}
bf1o4zei

bf1o4zei1#

是的,这是可能的。尝试添加子视图到您的图像视图,如下所示

let newImageView = UIImageView(image : croppedImage)
let labelView = UILabel(frame: CGRectMake(30, 30, 100, 20)) //adjust frame to change position of water mark or text
labelView.text = "my text"
newImageView.addSubview(labelView)
UIGraphicsBeginImageContext(newImageView.frame.size)
newImageView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
let watermarkedImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

相关问题