質問編集履歴
1
コードの記載の為
title
CHANGED
File without changes
|
body
CHANGED
@@ -6,16 +6,69 @@
|
|
6
6
|
```swift
|
7
7
|
vc.addImage(someView.snapShot())
|
8
8
|
```の所で 「Type "someView" has no member "snapShot"」のエラーになります。
|
9
|
-
普通に画像を添付する場合には
|
10
9
|
|
11
|
-
```Swift
|
12
|
-
vc.addImage(UIImage(named: "Twitter.png"))
|
13
|
-
```
|
14
10
|
|
15
|
-
等で良いと思うのですが、
|
16
|
-
|
17
11
|
```Swift
|
18
12
|
vc.addImage(someView.snapShot())
|
19
13
|
```スクリーンショットの場合は上記のコードであっているのでしょうか?
|
20
14
|
色々と書きなおしても、そこでエラーになってしまいます。
|
21
|
-
ご回答よろしくお願いします。
|
15
|
+
ご回答よろしくお願いします。
|
16
|
+
|
17
|
+
コードは以下になります。
|
18
|
+
|
19
|
+
```Swift
|
20
|
+
import UIKit
|
21
|
+
import Social
|
22
|
+
|
23
|
+
public extension UIView {
|
24
|
+
func snapShot() -> UIImage {
|
25
|
+
UIGraphicsBeginImageContextWithOptions(bounds.size, false, 1)
|
26
|
+
drawViewHierarchyInRect(bounds, afterScreenUpdates: true)
|
27
|
+
let image = UIGraphicsGetImageFromCurrentImageContext()
|
28
|
+
UIGraphicsEndImageContext()
|
29
|
+
return image
|
30
|
+
}
|
31
|
+
}
|
32
|
+
|
33
|
+
class GameViewController: UIViewController {
|
34
|
+
|
35
|
+
var myComposeView : SLComposeViewController!
|
36
|
+
var myTwitterButton: UIButton!
|
37
|
+
|
38
|
+
override func viewDidLoad() {
|
39
|
+
super.viewDidLoad()
|
40
|
+
|
41
|
+
|
42
|
+
// Twitterボタン.
|
43
|
+
myTwitterButton = UIButton()
|
44
|
+
myTwitterButton.frame = CGRectMake(0,0,100,100)
|
45
|
+
myTwitterButton.layer.masksToBounds = true
|
46
|
+
myTwitterButton.setTitle("Twitter", forState: UIControlState.Normal)
|
47
|
+
myTwitterButton.titleLabel?.font = UIFont.systemFontOfSize(CGFloat(20))
|
48
|
+
myTwitterButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
|
49
|
+
myTwitterButton.layer.position = CGPoint(x: self.view.frame.width/2, y:self.view.frame.height/2)
|
50
|
+
myTwitterButton.tag = 1
|
51
|
+
myTwitterButton.addTarget(self, action: #selector(GameViewController.onPostToTwitter(_:)), forControlEvents: .TouchUpInside)
|
52
|
+
|
53
|
+
// buttonをviewに追加.
|
54
|
+
self.view.addSubview(myTwitterButton)
|
55
|
+
}
|
56
|
+
|
57
|
+
// ボタンイベント.
|
58
|
+
func onPostToTwitter(sender : AnyObject) {
|
59
|
+
|
60
|
+
// SLComposeViewControllerのインスタンス化.
|
61
|
+
// ServiceTypeをTwitterに指定.
|
62
|
+
myComposeView = SLComposeViewController(forServiceType: SLServiceTypeTwitter)
|
63
|
+
|
64
|
+
// 投稿するテキストを指定.
|
65
|
+
myComposeView.setInitialText("Twitter Test")
|
66
|
+
|
67
|
+
// 投稿する画像を指定.
|
68
|
+
myComposeView.addImage(UIView.snapShot())//エラーの部分
|
69
|
+
// myComposeViewの画面遷移.
|
70
|
+
self.presentViewController(myComposeView, animated: true, completion: nil)
|
71
|
+
}
|
72
|
+
|
73
|
+
}
|
74
|
+
```
|