繁体   English   中英

如何使用具有宽高比的图像截取 UIImageiew 的屏幕截图?

[英]How to take screen shot of UIImageiew using image with aspect fit?

我想从 imageview 以适合图像纵横比的方式拍摄屏幕截图。 我尝试使用此代码,但不会得到实际结果。 我想得到实际图像大小的屏幕截图

func captureScreen() -> UIImage
    {
        let layer =  self.layer
        let scale = UIScreen.main.scale
        UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
        guard let context = UIGraphicsGetCurrentContext() else {return UIImage.init(named: "")!}
        layer.render(in:context)
        let screenshotImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
                
        return screenshotImage!
    }

这就是我得到的在此处输入图像描述 我想用完整的实际图像大小截屏请帮助我

    //MARK:- Calculate ImageviewWith image size
    func convertImageViewToImagebounds(image:UIImage, imageView:UIImageView) {
            if image.size.height > image.size.width {
                let ratio = image.size.height / image.size.width
                let newWidth = imageView.frame.size.height / ratio
                imageView.frame.size = CGSize(width: newWidth, height: imageView.frame.size.height)
                imageView.center = CGPoint(x: viewImgViewContainer.frame.width / 2.0, y: viewImgViewContainer.frame.height / 2.0)
            }else if image.size.height < image.size.width {
                let ratio = image.size.width / image.size.height
                let newHeight = imageView.frame.size.width / ratio
                imageView.frame.size = CGSize(width: imageView.frame.size.width, height: newHeight)
                imageView.center = CGPoint(x: viewImgViewContainer.frame.width / 2.0, y: viewImgViewContainer.frame.height / 2.0)
            }else {
                let ratio = image.size.width / image.size.height
                let newHeight = image.size.width / ratio
                imageView.frame.size = CGSize(width: image.size.width, height: newHeight)
                imageView.center = CGPoint(x: viewImgViewContainer.frame.width / 2.0, y: viewImgViewContainer.frame.height / 2.0)
            }
    }

看看这里:

this function will return your imageview size as image have and viewImgViewContainer is parent view of imageview to set imageview in center.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM