【问题标题】:take a partial screenshot on button press按下按钮时截取部分截图
【发布时间】:2017-06-13 23:22:00
【问题描述】:

我正在尝试通过按下按钮截取部分屏幕截图并将其保存到相机胶卷。

我有从另一个问题中找到的代码,但按下按钮会使应用程序崩溃。

// Declare the snapshot boundaries
let top: CGFloat = 100
let bottom: CGFloat = 0

// The size of the cropped image
let size = CGSize(width: view.frame.size.width, height: view.frame.size.height - top - bottom)

// Start the context
UIGraphicsBeginImageContext(size)

// we are going to use context in a couple of places
let context = UIGraphicsGetCurrentContext()!

// Transform the context so that anything drawn into it is displaced "top" pixels up

context.translateBy(x: 0, y: -top)

// Draw the view into the context (this is the snapshot)
view.layer.render(in: context)
let snapshot = UIGraphicsGetImageFromCurrentImageContext()

// End the context (this is required to not leak resources)
UIGraphicsEndImageContext()

// Save to photos
UIImageWriteToSavedPhotosAlbum(snapshot!, nil, nil, nil)

【问题讨论】:

  • 你调试这段代码是哪一行崩溃了?您是否为按钮正确设置了插座或添加了选择器?
  • 能否分享一下崩溃日志。
  • @iasla 问题解决了吗?
  • 还没有。我正在研究我的应用程序的其他部分,直到找到解决方案

标签: ios swift xcode swift3


【解决方案1】:

在此函数中使用参数调用您的屏幕截图视图:-

func imageWithView(view: UIView) -> UIImage {
     UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0)
     view.layer.renderInContext(UIGraphicsGetCurrentContext()!)
     let img = UIGraphicsGetImageFromCurrentImageContext()
     UIGraphicsEndImageContext()
     return img
}

【讨论】:

    猜你喜欢
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-19
    相关资源
    最近更新 更多