我想知道我的iPhone应用程序如何利用特定的屏幕截图UIView作为UIImage.
我试过这段代码,但我得到的只是一张空白图片.
UIGraphicsBeginImageContext(CGSizeMake(320,480));
CGContextRef context = UIGraphicsGetCurrentContext();
[myUIView.layer drawInContext:context];
UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)
myUIView尺寸为320x480,并且有一些子视图.这样做的正确方法是什么?
如何截取 AVplayerLayer 的截图。我尝试使用以下代码效果很好,它可以按原样捕获整个视图
func screenShotMethod() {
let window = UIApplication.shared.delegate!.window!!
//capture the entire window into an image
UIGraphicsBeginImageContextWithOptions(window.bounds.size, false, UIScreen.main.scale)
window.drawHierarchy(in: window.bounds, afterScreenUpdates: false)
let windowImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//now position the image x/y away from the top-left corner to get the portion we want
UIGraphicsBeginImageContext(view.frame.size)
windowImage?.draw(at: CGPoint(x: -view.frame.origin.x, y: -view.frame.origin.y))
let croppedImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext();
//embed image in an imageView, supports transforms.
let resultImageView = UIImageView(image: croppedImage)
UIImageWriteToSavedPhotosAlbum(croppedImage, nil, nil, nil)
}
Run Code Online (Sandbox Code Playgroud)
但问题是当我尝试在 iPhone(设备)上运行相同的代码时,它返回黑色图像。我不知道出了什么问题
任何建议都会非常有帮助!