teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

5

コードの変更

2019/01/08 15:00

投稿

mimamo
mimamo

スコア44

title CHANGED
File without changes
body CHANGED
@@ -41,7 +41,7 @@
41
41
 
42
42
  @IBAction func preView(_ sender: UIButton) {
43
43
 
44
- createPdfFromView(aView: canvasView, saveToDocumentsWithFileName: "test.pdf")
44
+ createPdfFromView(aView: canvasView, saveToDocumentsWithFileName: "test")
45
45
 
46
46
  }
47
47
 
@@ -67,8 +67,8 @@
67
67
 
68
68
 
69
69
  // 読み込み
70
- let filePath0 = UserDefaults.standard.string(forKey:"filePath")
70
+ let Path = UserDefaults.standard.string(forKey:"filePath")
71
- let pdfURL = URL(fileURLWithPath: filePath0!)
71
+ let pdfURL = URL(fileURLWithPath: Path)
72
72
 
73
73
 
74
74
  let document = PDFDocument(url: pdfURL)

4

コードの変更

2019/01/08 15:00

投稿

mimamo
mimamo

スコア44

title CHANGED
File without changes
body CHANGED
@@ -31,8 +31,9 @@
31
31
  debugPrint(documentsFileName)
32
32
  pdfData.write(toFile: documentsFileName, atomically: true)
33
33
  print("プレビュー作成おわり")
34
- // UserDefaultsに保存する
34
+ // 保存
35
- userDefaults.set(documentsFileName, forKey: "pdfname")
35
+ UserDefaults.standard.set(documentsFileName, forKey:"filePath")
36
+
36
37
  }
37
38
  }
38
39
 
@@ -40,7 +41,7 @@
40
41
 
41
42
  @IBAction func preView(_ sender: UIButton) {
42
43
 
43
- createPdfFromView(aView: canvasView, saveToDocumentsWithFileName: "createpdf")
44
+ createPdfFromView(aView: canvasView, saveToDocumentsWithFileName: "test.pdf")
44
45
 
45
46
  }
46
47
 
@@ -53,28 +54,23 @@
53
54
 
54
55
  class ViewController3: UIViewController {
55
56
 
56
- @IBOutlet weak var pdfView: PDFView!
57
+ @IBOutlet weak var pdfView: PDFView!
57
58
 
59
+
58
60
 
59
- // UserDefaults のインスタンス
60
- let userDefaults = UserDefaults.standard
61
61
 
62
-
63
62
  override func viewDidLoad() {
64
63
  super.viewDidLoad()
65
64
 
66
- // UserDefaultsから値を読み込む
67
- let str2:String = userDefaults.string(forKey: "pdfname")!
68
- print(str2)
69
65
 
70
- //let pdfView = PDFView(frame: view.frame)
66
+ let pdfView = PDFView(frame: view.frame)
71
- //保存場所
72
- let thePath = NSHomeDirectory()+str2+".pdf"
73
- // ローカルのPDFの場合
74
- let pdfURL = URL(fileURLWithPath : thePath)
75
67
 
76
-
77
68
 
69
+ // 読み込み
70
+ let filePath0 = UserDefaults.standard.string(forKey:"filePath")
71
+ let pdfURL = URL(fileURLWithPath: filePath0!)
72
+
73
+
78
74
  let document = PDFDocument(url: pdfURL)
79
75
  pdfView.document = document
80
76
  pdfView.backgroundColor = .lightGray
@@ -84,7 +80,7 @@
84
80
  // 表示モード
85
81
  pdfView.displayMode = .singlePageContinuous
86
82
 
87
- //view.addSubview(pdfView)
83
+ view.addSubview(pdfView)
88
84
 
89
85
  }
90
86
  }

3

コードの変更

2019/01/08 14:18

投稿

mimamo
mimamo

スコア44

title CHANGED
File without changes
body CHANGED
@@ -11,48 +11,69 @@
11
11
 
12
12
  pdfを作成するVC↓
13
13
  ```swift
14
- // UserDefaults のインスタンス
14
+ // UserDefaults のインスタンス
15
15
  let userDefaults = UserDefaults.standard
16
16
 
17
-
18
- @IBAction func preView(_ sender: UIButton) {
17
+ func createPdfFromView(aView: UIView, saveToDocumentsWithFileName fileName: String)
18
+ {
19
19
  print("プレビュー画面作成開始")
20
20
  let pdfData = NSMutableData()
21
- UIGraphicsBeginPDFContextToData(pdfData, canvasView.bounds, nil)
21
+ UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil)
22
22
  UIGraphicsBeginPDFPage()
23
23
 
24
24
  guard let pdfContext = UIGraphicsGetCurrentContext() else { return }
25
25
 
26
- canvasView.layer.render(in: pdfContext)
26
+ aView.layer.render(in: pdfContext)
27
27
  UIGraphicsEndPDFContext()
28
28
 
29
29
  if let documentDirectories = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
30
- let documentsFileName = documentDirectories + "/" + "createPdf"
30
+ let documentsFileName = documentDirectories + "/" + fileName
31
31
  debugPrint(documentsFileName)
32
32
  pdfData.write(toFile: documentsFileName, atomically: true)
33
+ print("プレビュー作成おわり")
33
34
  // UserDefaultsに保存する
34
- userDefaults.set(documentsFileName, forKey: "setpdf")
35
+ userDefaults.set(documentsFileName, forKey: "pdfname")
35
- print("プレビュー作成おわり")
36
36
  }
37
+ }
38
+
39
+
40
+
41
+ @IBAction func preView(_ sender: UIButton) {
37
42
 
43
+ createPdfFromView(aView: canvasView, saveToDocumentsWithFileName: "createpdf")
44
+
38
45
  }
46
+
39
47
  ```
40
48
 
41
49
  pdfを表示するVC
42
50
  ```swift
51
+ import UIKit
52
+ import PDFKit
53
+
54
+ class ViewController3: UIViewController {
55
+
56
+ @IBOutlet weak var pdfView: PDFView!
57
+
58
+
43
- // UserDefaults のインスタンス
59
+ // UserDefaults のインスタンス
44
60
  let userDefaults = UserDefaults.standard
45
61
 
62
+
46
63
  override func viewDidLoad() {
47
64
  super.viewDidLoad()
48
65
 
49
66
  // UserDefaultsから値を読み込む
50
- let str2:String = userDefaults.string(forKey: "setpdf")!
67
+ let str2:String = userDefaults.string(forKey: "pdfname")!
68
+ print(str2)
51
69
 
52
- let pdfView = PDFView(frame: view.frame)
70
+ //let pdfView = PDFView(frame: view.frame)
53
-
71
+ //保存場所
72
+ let thePath = NSHomeDirectory()+str2+".pdf"
54
73
  // ローカルのPDFの場合
55
- let pdfURL = URL(fileURLWithPath: Bundle.main.path(forResource: "str2", ofType: "pdf")!)
74
+ let pdfURL = URL(fileURLWithPath : thePath)
75
+
76
+
56
77
 
57
78
  let document = PDFDocument(url: pdfURL)
58
79
  pdfView.document = document
@@ -63,7 +84,8 @@
63
84
  // 表示モード
64
85
  pdfView.displayMode = .singlePageContinuous
65
86
 
66
- view.addSubview(pdfView)
87
+ //view.addSubview(pdfView)
88
+
67
89
  }
68
90
  }
69
91
  ```
@@ -71,11 +93,10 @@
71
93
 
72
94
  ###エラー該当箇所
73
95
  ```
74
- // ローカルのPDFの場合
75
- let pdfURL = URL(fileURLWithPath: Bundle.main.path(forResource: "str2", ofType: "pdf")!)
96
+ class AppDelegate: UIResponder, UIApplicationDelegate{
76
97
 
77
98
  ```
78
- Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
99
+ Thread 1: signal SIGABRT
79
100
 
80
101
  ### 補足情報(FW/ツールのバージョンなど)
81
102
 

2

コードの変更

2019/01/08 06:43

投稿

mimamo
mimamo

スコア44

title CHANGED
File without changes
body CHANGED
@@ -11,58 +11,48 @@
11
11
 
12
12
  pdfを作成するVC↓
13
13
  ```swift
14
+ // UserDefaults のインスタンス
15
+ let userDefaults = UserDefaults.standard
16
+
17
+
14
- @IBAction func preView(_ sender: UIButton) {
18
+ @IBAction func preView(_ sender: UIButton) {
15
-
16
- print("プレビュー画面作成開始")
19
+ print("プレビュー画面作成開始")
17
-
18
- let pdfData = NSMutableData()
20
+ let pdfData = NSMutableData()
19
- UIGraphicsBeginPDFContextToData(pdfData, getphoto.bounds, nil)
21
+ UIGraphicsBeginPDFContextToData(pdfData, canvasView.bounds, nil)
20
- UIGraphicsBeginPDFPage()
22
+ UIGraphicsBeginPDFPage()
21
-
23
+
22
- guard let pdfContext = UIGraphicsGetCurrentContext() else { return }
24
+ guard let pdfContext = UIGraphicsGetCurrentContext() else { return }
23
-
25
+
24
- getphoto.layer.render(in: pdfContext)
26
+ canvasView.layer.render(in: pdfContext)
25
- UIGraphicsEndPDFContext()
27
+ UIGraphicsEndPDFContext()
26
-
28
+
27
- if let documentDirectories = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
29
+ if let documentDirectories = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
28
- let documentsFileName = documentDirectories + "/" + "/preview.pdf"
30
+ let documentsFileName = documentDirectories + "/" + "createPdf"
29
- debugPrint(documentsFileName)
31
+ debugPrint(documentsFileName)
30
- pdfData.write(toFile: documentsFileName, atomically: true)
32
+ pdfData.write(toFile: documentsFileName, atomically: true)
33
+ // UserDefaultsに保存する
34
+ userDefaults.set(documentsFileName, forKey: "setpdf")
35
+ print("プレビュー作成おわり")
31
- }
36
+ }
32
- print("プレビュー作成おわり")
37
+
33
- }
38
+ }
34
39
  ```
35
40
 
36
41
  pdfを表示するVC
37
42
  ```swift
43
+ // UserDefaults のインスタンス
44
+ let userDefaults = UserDefaults.standard
45
+
38
46
  override func viewDidLoad() {
39
47
  super.viewDidLoad()
40
48
 
49
+ // UserDefaultsから値を読み込む
41
- let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
50
+ let str2:String = userDefaults.string(forKey: "setpdf")!
42
51
 
43
- var fileNames: [String] {
52
+ let pdfView = PDFView(frame: view.frame)
44
- do {
45
- return try FileManager.default.contentsOfDirectory(atPath: documentPath)
46
- } catch {
47
- return []
48
- }
49
- }
50
53
 
51
- return fileNames.flatMap { fileName in
52
- do {
53
- var texts: [String] = try String(contentsOfFile: documentPath + "/" + fileName, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue)).lines
54
- texts = texts.deleteSpaceOnly(texts: texts)
55
- return texts
56
- } catch {
57
- return nil
58
- }
59
-
60
- }
61
-
62
- //PDFの表示
63
- let pdfView = PDFView(frame: view.frame)
64
54
  // ローカルのPDFの場合
65
- let pdfURL = URL(fileURLWithPath: Bundle.main.path(forResource: "metronetwork", ofType: "pdf")!)
55
+ let pdfURL = URL(fileURLWithPath: Bundle.main.path(forResource: "str2", ofType: "pdf")!)
66
56
 
67
57
  let document = PDFDocument(url: pdfURL)
68
58
  pdfView.document = document
@@ -74,18 +64,18 @@
74
64
  pdfView.displayMode = .singlePageContinuous
75
65
 
76
66
  view.addSubview(pdfView)
77
-
78
67
  }
79
-
68
+ }
80
69
  ```
81
70
 
82
71
 
83
- ###エラー、警告(3つ)
72
+ ###エラー該当箇所
84
73
  ```
85
- 1、Unexpected non-void return value in void function
86
- 2、No calls to throwing functions occur within 'try' expression
74
+ // ローカルのPDFの場合
87
- 3、'catch' block is unreachable because no errors are thrown in 'do' block
75
+ let pdfURL = URL(fileURLWithPath: Bundle.main.path(forResource: "str2", ofType: "pdf")!)
76
+
88
77
  ```
78
+ Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
89
79
 
90
80
  ### 補足情報(FW/ツールのバージョンなど)
91
81
 

1

コードの変更

2019/01/07 01:37

投稿

mimamo
mimamo

スコア44

title CHANGED
File without changes
body CHANGED
@@ -9,6 +9,84 @@
9
9
 
10
10
 
11
11
 
12
+ pdfを作成するVC↓
13
+ ```swift
14
+ @IBAction func preView(_ sender: UIButton) {
15
+
16
+ print("プレビュー画面作成開始")
17
+
18
+ let pdfData = NSMutableData()
19
+ UIGraphicsBeginPDFContextToData(pdfData, getphoto.bounds, nil)
20
+ UIGraphicsBeginPDFPage()
21
+
22
+ guard let pdfContext = UIGraphicsGetCurrentContext() else { return }
23
+
24
+ getphoto.layer.render(in: pdfContext)
25
+ UIGraphicsEndPDFContext()
26
+
27
+ if let documentDirectories = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first {
28
+ let documentsFileName = documentDirectories + "/" + "/preview.pdf"
29
+ debugPrint(documentsFileName)
30
+ pdfData.write(toFile: documentsFileName, atomically: true)
31
+ }
32
+ print("プレビュー作成おわり")
33
+ }
34
+ ```
35
+
36
+ pdfを表示するVC
37
+ ```swift
38
+ override func viewDidLoad() {
39
+ super.viewDidLoad()
40
+
41
+ let documentPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
42
+
43
+ var fileNames: [String] {
44
+ do {
45
+ return try FileManager.default.contentsOfDirectory(atPath: documentPath)
46
+ } catch {
47
+ return []
48
+ }
49
+ }
50
+
51
+ return fileNames.flatMap { fileName in
52
+ do {
53
+ var texts: [String] = try String(contentsOfFile: documentPath + "/" + fileName, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue)).lines
54
+ texts = texts.deleteSpaceOnly(texts: texts)
55
+ return texts
56
+ } catch {
57
+ return nil
58
+ }
59
+
60
+ }
61
+
62
+ //PDFの表示
63
+ let pdfView = PDFView(frame: view.frame)
64
+ // ローカルのPDFの場合
65
+ let pdfURL = URL(fileURLWithPath: Bundle.main.path(forResource: "metronetwork", ofType: "pdf")!)
66
+
67
+ let document = PDFDocument(url: pdfURL)
68
+ pdfView.document = document
69
+ pdfView.backgroundColor = .lightGray
70
+
71
+ // PDFの拡大率を調整する
72
+ pdfView.autoScales = true
73
+ // 表示モード
74
+ pdfView.displayMode = .singlePageContinuous
75
+
76
+ view.addSubview(pdfView)
77
+
78
+ }
79
+
80
+ ```
81
+
82
+
83
+ ###エラー、警告(3つ)
84
+ ```
85
+ 1、Unexpected non-void return value in void function
86
+ 2、No calls to throwing functions occur within 'try' expression
87
+ 3、'catch' block is unreachable because no errors are thrown in 'do' block
88
+ ```
89
+
12
90
  ### 補足情報(FW/ツールのバージョンなど)
13
91
 
14
92
  Swiftのversionは4.1.2