简体   繁体   English

带有UIScrollView的UIView屏幕截图

[英]Screenshot of UIView with UIScrollView inside

I have a UIView which contains an UIScrollView inside, actually it contains an UIImageScrollView, a class which helps me to zoom an UIImage, and I want to take a screenshot of UIView with zoomed UIImage, I found this function: 我有一个UIView,其中包含一个UIScrollView,实际上它包含一个UIImageScrollView,一个可帮助我缩放UIImage的类,并且我想使用缩放后的UIImage拍摄UIView的屏幕快照,发现此功能:

 func captureCrop() -> UIImage {
    UIGraphicsBeginImageContextWithOptions(self.viewCapturedFoto.frame.size, true, 0.0)
    self.viewCapturedFoto.layer.renderInContext(UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image;
}

but when the screenshot is done the new UIImage is showed without zoomed image, I mean the UIImage is still not zoomed even if I zoomed it. 但是,截屏完成后,新的UIImage会显示没有缩放的图像,这意味着即使缩放了UIImage也不会缩放。 I don't know if I have explained it right enough 我不知道我解释得是否正确

I believe your issue is with the size argument you are passing into your UIGraphicsBeginImageContextWithOptions function. 我相信您的问题在于传递给UIGraphicsBeginImageContextWithOptions函数的size参数。 Right now you are passing in the size of the entire frame, which in scroll views, includes the off the screen portion as well. 现在,您传入的是整个帧的大小,在滚动视图中,还包括屏幕外的部分。 You might try changing the size argument to the bounds of the device screen or, worst case, putting your UIImageScrollView inside another view and using the bounds of this container view to set the image size. 您可以尝试将size参数更改为设备屏幕的边界,或者在最坏的情况下,将UIImageScrollView放在另一个视图中,并使用此容器视图的边界来设置图像大小。

Documentation for your reference: 供您参考的文档:

Declaration 宣言

func UIGraphicsBeginImageContextWithOptions(_ size: CGSize, _ opaque: Bool, _ scale: CGFloat) func UIGraphicsBeginImageContextWithOptions(_ size:CGSize,_ opaque:Bool,_ scale:CGFloat)

Parameters 参量

size: The size (measured in points) of the new bitmap context. size:新位图上下文的大小(以磅为单位)。 This represents the size of the image returned by the UIGraphicsGetImageFromCurrentImageContext function. 这表示UIGraphicsGetImageFromCurrentImageContext函数返回的图像的大小。 To get the size of the bitmap in pixels, you must multiply the width and height values by the value in the scale parameter. 若要获取位图的大小(以像素为单位),必须将width和height值乘以scale参数中的值。

try this. 尝试这个。 it returns an UIView tho 它返回一个UIView

let screenShot = self.viewCapturedFoto.snapshotViewAfterScreenUpdates(true)

for more info visit this section of Apple Docs 有关更多信息,请访问Apple Docs的这一部分

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

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