gpt4 book ai didi

ios - Objective-C:捕获自定义框架内所有 View 的屏幕截图

转载 作者:可可西里 更新时间:2023-11-01 05:02:49 26 4
gpt4 key购买 nike

我有一款游戏,用户可以在其中创建自定义级别并将其上传到我的服务器供其他用户玩,我想在用户测试他/她的级别以上传到我的服务器之前获取“操作区域”的屏幕截图作为一种“预览图像”。

我知道如何获取整个 View 的屏幕截图,但我想将其定义为自定义框架。考虑下图:

Action Area

我只想截取红色区域“操作区域”的屏幕截图。我能做到吗?

最佳答案

只需要将你想要捕获的区域做成一个矩形,然后在方法中传递这个矩形即可。

swift 3.x:

extension UIView {
func imageSnapshot() -> UIImage {
return self.imageSnapshotCroppedToFrame(frame: nil)
}

func imageSnapshotCroppedToFrame(frame: CGRect?) -> UIImage {
let scaleFactor = UIScreen.main.scale
UIGraphicsBeginImageContextWithOptions(bounds.size, false, scaleFactor)
self.drawHierarchy(in: bounds, afterScreenUpdates: true)
var image: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()

if let frame = frame {
let scaledRect = frame.applying(CGAffineTransform(scaleX: scaleFactor, y: scaleFactor))

if let imageRef = image.cgImage!.cropping(to: scaledRect) {
image = UIImage(cgImage: imageRef)
}
}
return image
}
}

//How to call :
imgview.image = self.view.imageSnapshotCroppedToFrame(frame: CGRect.init(x: 0, y: 0, width: 320, height: 100))

Objective-C :

-(UIImage *)captureScreenInRect:(CGRect)captureFrame 
{
CALayer *layer;
layer = self.view.layer;
UIGraphicsBeginImageContext(self.view.bounds.size);
CGContextClipToRect (UIGraphicsGetCurrentContext(),captureFrame);
[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return screenImage;
}

//How to call :
imgView.image = [self captureScreenInRect:CGRectMake(0, 0, 320, 100)];

关于ios - Objective-C:捕获自定义框架内所有 View 的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18679787/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com