裁剪截图并分享?

时间:2015-08-26 13:42:07

标签: ios swift uigraphicscontext

我已经完成了没有导航栏的完整屏幕截图的编码,但我也不想要“激励我”按钮。

enter image description here

此外,如果有人知道如何将剪辑截图分享到Facebook?

以下是截图的代码:

System.out.println(sdff.format(start)+"..."+sdff.format(end));

2 个答案:

答案 0 :(得分:1)

确保“激励我”按钮不在您正在截屏的视图的层次结构中。将其移动到单独的视图,然后使用您已有的相同代码。这将完全符合您的要求。

或者,如果您想裁剪整张照片,可以使用以下内容,请替换heightOfButton中的值以适应:

var heightOfButton: CGFloat = 100
var size = UIScreen.mainScreen().bounds.size
size.height -= heightOfButton
UIGraphicsBeginImageContextWithOptions(size,false,0);
self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

答案 1 :(得分:1)

在截取屏幕截图之前隐藏UIButton,并在截取屏幕截图后取消隐藏。

func screenshot() {
    // Hide button
    myButton.alpha = 0.0

    // Take screenshot
    UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size,false,0)
    self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
    var image:UIImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    // Unhide button
    myButton.alpha = 1.0
}

要将图像分享到Facebook,您可以使用SLComposeViewController

在旁注中,您不需要在Swift的每一行末尾使用;