2017-06-12 41 views
0

我試圖用按鈕按下一個部分屏幕截圖並將其保存到相機膠捲。按下按鈕上的部分截圖

我有另一個問題,但按下按鈕崩潰應用程序的代碼。

// 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) 
+0

你調試此代碼行中墜毀?你是否設置了插座或正確添加了選擇按鈕? –

+1

請您分享一下崩潰日誌。 – Developer

+0

@iasla問題解決? –

回答

0

調用具有參數屏幕快照視圖中此功能: -

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