Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/108.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何使用具有方面匹配的图像拍摄UIImageView的屏幕快照?_Ios_Swift_Iphone_Image_Screenshot - Fatal编程技术网

Ios 如何使用具有方面匹配的图像拍摄UIImageView的屏幕快照?

Ios 如何使用具有方面匹配的图像拍摄UIImageView的屏幕快照?,ios,swift,iphone,image,screenshot,Ios,Swift,Iphone,Image,Screenshot,我想从imageview中拍摄屏幕截图,图像方面适合。我尝试使用此代码,但不会得到实际结果。我想得到实际图像大小的屏幕截图 func captureScreen() -> UIImage { let layer = self.layer let scale = UIScreen.main.scale UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, sca

我想从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!
    }
这就是我得到的 我想用实际的屏幕截图与完整的实际图像大小 请帮助我

//标记:-使用图像大小计算ImageView
    //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)
            }
    }
func convertImageViewToImagebounds(图像:UIImage,图像视图:UIImageView){ 如果image.size.height>image.size.width{ let ratio=image.size.height/image.size.width 设newWidth=imageView.frame.size.height/ratio imageView.frame.size=CGSize(宽度:newWidth,高度:imageView.frame.size.height) imageView.center=CGPoint(x:viewmgviewcontainer.frame.width/2.0,y:viewmgviewcontainer.frame.height/2.0) }否则,如果image.size.height
请看这里:


此函数将返回您的
imageview
大小作为
image
have,并且viewImgViewContainer是imageview的父视图,用于将imageview设置在中间。

您的图像大小是layer.frame.size?什么是self.layer?是的,那是我的imageview层@eltomato您可以使用
imagview.contentMode=.aspectfill
实现它,我问题中的屏幕截图已经从set.aspectfill@RB's@KamleshShingarakhiya但在这里,您在问题中提到了
方面适合性
。谢谢。它会变大