2017-04-12 15 views
1

自分の携帯電話で共有をクリックできる機能を有効にしたい場合は、デフォルトで写真を添付し​​たメッセージを友人に送ることができますアプリ(ユーザーが共有を開始する直前)。私はこれを行う方法を理解することができません - それを行う方法を検索するたびに、電話のフォトライブラリまたはカメラで画像を共有する方法に関するチュートリアル/回答が得られます。これを行う方法に関するアイデア!iOS/swiftの現在のスクリーンショットのスクリーンショットを取得する

+1

スクリーンショット機能は既にすべてのiPhoneに搭載されています。だからスクリーンショットを撮って共有してください。 – KAR

答えて

2

//、ユーザーが共有を打つ前に自分の画面を共有するには、画像

var imagesToShare = [AnyObject]() 
imagesToShare.append(image) 

let activityViewController = UIActivityViewController(activityItems: imagesToShare , applicationActivities: nil) 
activityViewController.popoverPresentationController?.sourceView = self.view 
presentViewController(activityViewController, animated: true, completion: nil) 
3
UIGraphicsBeginImageContext(self.view.bounds.size) 
self.view.drawHierarchy(in: view.bounds, afterScreenUpdates: false) 
let image = UIGraphicsGetImageFromCurrentImageContext() 
UIGraphicsEndImageContext() 
+0

このコードを使用してuiimageを取得し、次に共有に進むことができます。 –

1

を共有するために最初にあなたが必要とされるスクリーンショットから

UIGraphicsBeginImageContext(view.frame.size) 
view.layer.renderInContext(UIGraphicsGetCurrentContext()) 
let image = UIGraphicsGetImageFromCurrentImageContext() 
UIGraphicsEndImageContext() 

//を画像を作成します画面のビューをイメージに変更して共有することができます。

ビューを画像に変更するには、この拡張子をコードに追加します。

//UIView extension which converts the UIView into an image. 
extension UIView { 
    func toImage() -> UIImage { 
     UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.mainScreen().scale) 

     drawViewHierarchyInRect(self.bounds, afterScreenUpdates: true) 

     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return image 
    } 
} 

例えば、そこにあなたのViewControllerのビューを渡す:

let imageToShare = self.view.toImage() 

    let activityItems : NSMutableArray = []() 
     activityItems.addObject(imageToShare) 


    let activityVC = UIActivityViewController(activityItems:activityItems as [AnyObject] , applicationActivities: nil) 
       self.presentViewController(activityVC, animated: true, completion: nil) 

を私はこれがあなたを助け願っています。

関連する問題