-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewController.swift
executable file
·749 lines (506 loc) · 30.3 KB
/
ViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
//
// ViewController.swift
// StarCard
//
// Created by Semper Idem on 14-10-22.
// Copyright (c) 2014年 星夜暮晨. All rights reserved.
//
import UIKit
class EmailMenuItem: UIMenuItem{
var indexPath: IndexPath!
}
class ViewController: UIViewController,SectionHeaderViewDelegate,UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource{
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var my_view: UIView!
@IBOutlet weak var searchBar: UISearchBar!
var countrySearchController = UISearchController()
let SectionHeaderViewIdentifier = "SectionHeaderViewIdentifier"
var plays:NSArray!
var sectionInfoArray:NSMutableArray!
var pinchedIndexPath:IndexPath!
var opensectionindex:Int!
var initialPinchHeight:CGFloat!
var playe:NSMutableArray?
var sectionHeaderView:SectionHeaderView!
//数据源
var data:NSMutableArray?
//搜索匹配的结果
var filtered:[String] = [String]()
//是否显示搜索结果
var shouldShowSearchResults = false
var str_arr:String = ""
var arr_message:[String] = [String]()
var search_text = ""
var string_cell = ""
var arr_number:[Int] = [Int]()
var arr_rows:[Int] = [Int]()
var count_section = 0
var arr_str:[String] = [String]()
var arr_section:[String] = [String]()
var arr_arr:[[String]] = [[String]]()
var actionButton:ActionButton!
var tb_bgname = UIImage(named: "launchScreen")
var imagePickerController = UIImagePickerController()
var imagePickerController_2 = UIImagePickerController() // 图片控件
//当缩放手势同时改变了所有单元格高度时使用uniformRowHeight
var uniformRowHeight: Int!
let DefaultRowHeight = 88
let HeaderHeight = 48
override func viewDidLoad() {
super.viewDidLoad()
// //注册点击事件
// view.addGestureRecognizer(UITapGestureRecognizer(target:self, action:#selector(Login.handleTap(_:))))
view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap(_:))))
//初始化searchBar
self.searchBar.backgroundColor = UIColor(red:0.02, green:0.48, blue:1, alpha:0)
self.searchBar.delegate = self
self.tableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0)
my_view.backgroundColor = UIColor(red:0.02, green:0.48, blue:1, alpha:0)
// 设置Header的高度
self.tableView.sectionHeaderHeight = CGFloat(HeaderHeight)
self.tableView.rowHeight = 88;
// 分节信息数组在viewWillUnload方法中将被销毁,因此在这里设置Header的默认高度是可行的。如果您想要保留分节信息等内容,可以在指定初始化器当中设置初始值。
self.uniformRowHeight = DefaultRowHeight
self.opensectionindex = NSNotFound
//加载首项
let sectionHeaderNib: UINib = UINib(nibName: "SectionHeaderView", bundle: nil)
self.tableView.register(sectionHeaderNib, forHeaderFooterViewReuseIdentifier: SectionHeaderViewIdentifier)
plays = played()
print("最后的")
print(plays)
var base = BaseClass()
var bg_name = base.cacheGetArray("bgname")
var myName:AnyObject? = NSKeyedUnarchiver.unarchiveObject(with: bg_name as! Data) as AnyObject?
self.tb_bgname = myName as? UIImage
let tb_img = UIImageView(image: self.tb_bgname)
self.tableView.backgroundView = tb_img
let twitterImage = UIImage(named: "logout.png")!
let plusImage = UIImage(named: "wallpaper.png")!
let twitter = ActionButtonItem(title: "退出登录", image: twitterImage)
twitter.action = { item in self.shareclick() }
let google = ActionButtonItem(title: "更换背景", image: plusImage)
google.action = { item in self.replace() }
actionButton = ActionButton(attachedToView: self.view, items: [twitter, google])
actionButton.action = { button in button.toggleMenu() }
actionButton.setTitle("+", forState: UIControlState())
actionButton.backgroundColor = UIColor(red: 238.0/255.0, green: 130.0/255.0, blue: 34.0/255.0, alpha:1.0)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override var canBecomeFirstResponder : Bool {
return true
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if plays == [] {
warring()
}
// 检查分节信息数组是否已被创建,如果其已创建,则再检查节的数量是否仍然匹配当前节的数量。通常情况下,您需要保持分节信息与单元格、分节格同步u过您要允许在表视图中编辑信息,您需要在编辑操作中适当更新分节信息。
if self.sectionInfoArray == nil || self.sectionInfoArray.count != self.numberOfSections(in: self.tableView) {
//对于每个场次来说,需要为每个单元格设立一个一致的、包含默认高度的SectionInfo对象。
let infoArray = NSMutableArray()
for play in self.plays {
var i = 0
var dic = (play as! Play).quotations
let sectionInfo = SectionInfo()
sectionInfo.play = play as! Play
sectionInfo.open = false
let defaultRowHeight = DefaultRowHeight
//获取行数
let countOfQuotations = sectionInfo.play.quotations.count
//为每行设置高度
for i in 0 ..< countOfQuotations {
sectionInfo.insertObject(defaultRowHeight as AnyObject, inRowHeightsAtIndex: i)
}
print("测试countOfQuotations")
infoArray.add(sectionInfo)
}
self.sectionInfoArray = infoArray
}
}
func numberOfSections(in tableView: UITableView) -> Int {
// 这个方法返回 tableview 有多少个section
return self.plays.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if self.search_text == "" {
// 这个方法返回对应的section有多少个元素,也就是多少行
let sectionInfo: SectionInfo = self.sectionInfoArray[section] as! SectionInfo
let numStoriesInSection = sectionInfo.play.quotations.count
let sectionOpen = sectionInfo.open!
//如果sectionOpen = false返回0,为true 则返回相应数量
print("测试section11111111",numStoriesInSection)
return sectionOpen ? numStoriesInSection : 0
}
else{
let sectionInfo:SectionInfo = self.sectionInfoArray[section] as! SectionInfo
let numStoriesInSection = self.arr_rows[section]
var sectionOpen = sectionInfo.open!
//sectionOpen = true
//如果sectionOpen = false返回0,为true 则返回相应数量
print("测试section11111111",numStoriesInSection)
return sectionOpen ? numStoriesInSection : 0
}
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// 返回指定的row 的cell。这个地方是比较关键的地方,一般在这个地方来定制各种个性化的 cell元素。这里只是使用最简单最基本的cell 类型。其中有一个主标题 cell.textLabel 还有一个副标题cell.detailTextLabel, 还有一个 image在最前头 叫cell.imageView. 还可以设置右边的图标,通过cell.accessoryType 可以设置是饱满的向右的蓝色箭头,还是单薄的向右箭头,还是勾勾标记。
let QuoteCellIdentifier = "QuoteCellIdentifier"
let cell: QuoteCell = tableView.dequeueReusableCell(withIdentifier: QuoteCellIdentifier) as! QuoteCell
if self.search_text == "" {
//部门
let play:Play = (self.sectionInfoArray[(indexPath as NSIndexPath).section] as! SectionInfo).play
//将值赋给cell
//部门成员
cell.quotation = play.quotations[(indexPath as NSIndexPath).row] as! Quotation
//部门成员详细信息
cell.setTheQuotation(cell.quotation)
cell.backgroundColor = UIColor(red:0.02, green:0.48, blue:1, alpha:0)
print("测试33333",play.quotations)
}
else{
// self.arr_section.append(filtered[self.count_section])
// self.count_section = self.count_section + 1
// print("测试count_section",self.count_section)
self.arr_section = self.arr_arr[(indexPath as NSIndexPath).section]
cell.filtered = self.arr_section[(indexPath as NSIndexPath).row]
cell.setSearchQuatation(cell.filtered)
cell.backgroundColor = UIColor(red:0.02, green:0.48, blue:1, alpha:0)
}
return cell
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
// 返回指定的 section header 的view,如果没有,这个函数可以不返回view
let sectionHeaderView: SectionHeaderView = self.tableView.dequeueReusableHeaderFooterView(withIdentifier: SectionHeaderViewIdentifier) as! SectionHeaderView
let sectionInfo: SectionInfo = self.sectionInfoArray[section] as! SectionInfo
sectionInfo.headerView = sectionHeaderView
sectionHeaderView.titleLabel.text = sectionInfo.play.name
sectionHeaderView.disclosureButton.isSelected = false
sectionHeaderView.backgroundColor = UIColor(red:0.02, green:0.48, blue:1, alpha:0)
sectionHeaderView.section = section
sectionHeaderView.delegate = self
return sectionHeaderView
}
// override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
// // 这个方法返回指定的 row 的高度
// let sectionInfo: SectionInfo = self.sectionInfoArray[(indexPath as NSIndexPath).section] as! SectionInfo
//
// return CGFloat(sectionInfo.objectInRowHeightsAtIndex((indexPath as NSIndexPath).row) as! NSNumber)
// //又或者,返回单元格的行高
// }
// _________________________________________________________________________
// SectionHeaderViewDelegate
func sectionHeaderView(_ sectionHeaderView: SectionHeaderView, sectionOpened: Int) {
if self.search_text == "" {
//获取哪个section被打开
var sectionInfo: SectionInfo = self.sectionInfoArray[sectionOpened] as! SectionInfo
print("测试sectionInfoArray",self.sectionInfoArray)
print("测试sectionOpened",sectionOpened)
sectionInfo.open = true
//创建一个包含单元格索引路径的数组来实现插入单元格的操作:这些路径对应当前节的每个单元格
var countOfRowsToInsert = sectionInfo.play.quotations.count
var indexPathsToInsert = NSMutableArray()
for i in 0 ..< countOfRowsToInsert {
indexPathsToInsert.add(IndexPath(row: i, section: sectionOpened))
}
// 创建一个包含单元格索引路径的数组来实现删除单元格的操作:这些路径对应之前打开的节的单元格
var indexPathsToDelete = NSMutableArray()
var previousOpenSectionIndex = self.opensectionindex
if previousOpenSectionIndex != NSNotFound {
var previousOpenSection: SectionInfo = self.sectionInfoArray[previousOpenSectionIndex!] as! SectionInfo
previousOpenSection.open = false
previousOpenSection.headerView.toggleOpenWithUserAction(false)
var countOfRowsToDelete = previousOpenSection.play.quotations.count
for i in 0 ..< countOfRowsToDelete {
indexPathsToDelete.add(IndexPath(row: i, section: previousOpenSectionIndex!))
}
}
// 设计动画,以便让表格的打开和关闭拥有一个流畅(很屌)的效果
var insertAnimation: UITableViewRowAnimation
var deleteAnimation: UITableViewRowAnimation
if previousOpenSectionIndex == NSNotFound || sectionOpened < previousOpenSectionIndex! {
insertAnimation = UITableViewRowAnimation.top
deleteAnimation = UITableViewRowAnimation.bottom
}else{
insertAnimation = UITableViewRowAnimation.bottom
deleteAnimation = UITableViewRowAnimation.top
}
// 应用单元格的更新
self.tableView.beginUpdates()
self.tableView.deleteRows(at: indexPathsToDelete as AnyObject as! [IndexPath], with: deleteAnimation)
self.tableView.insertRows(at: indexPathsToInsert as AnyObject as! [IndexPath], with: insertAnimation)
self.opensectionindex = sectionOpened
self.tableView.endUpdates()
}
else{
// 在表格关闭的时候,创建一个包含单元格索引路径的数组,接下来从表格中删除这些行
let sectionInfo: SectionInfo = self.sectionInfoArray[sectionOpened] as! SectionInfo
sectionInfo.open = false
let countOfRowsToDelete = self.tableView.numberOfRows(inSection: sectionOpened)
if countOfRowsToDelete > 0 {
let indexPathsToDelete = NSMutableArray()
for i in 0 ..< countOfRowsToDelete {
indexPathsToDelete.add(IndexPath(row: i, section: sectionOpened))
}
self.tableView.deleteRows(at: indexPathsToDelete as AnyObject as! [IndexPath], with: UITableViewRowAnimation.top)
}
self.opensectionindex = NSNotFound
}
}
func sectionHeaderView(_ sectionHeaderView: SectionHeaderView, sectionClosed: Int) {
if self.search_text == "" {
// 在表格关闭的时候,创建一个包含单元格索引路径的数组,接下来从表格中删除这些行
let sectionInfo: SectionInfo = self.sectionInfoArray[sectionClosed] as! SectionInfo
sectionInfo.open = false
let countOfRowsToDelete = self.tableView.numberOfRows(inSection: sectionClosed)
if countOfRowsToDelete > 0 {
let indexPathsToDelete = NSMutableArray()
for i in 0 ..< countOfRowsToDelete {
indexPathsToDelete.add(IndexPath(row: i, section: sectionClosed))
}
self.tableView.deleteRows(at: indexPathsToDelete as AnyObject as! [IndexPath], with: UITableViewRowAnimation.top)
}
self.opensectionindex = NSNotFound
}
else{
//获取哪个section被打开
var sectionInfo: SectionInfo = self.sectionInfoArray[sectionClosed] as! SectionInfo
sectionInfo.open = true
//创建一个包含单元格索引路径的数组来实现插入单元格的操作:这些路径对应当前节的每个单元格
var countOfRowsToInsert = self.arr_rows[sectionClosed]
var indexPathsToInsert = NSMutableArray()
for i in 0 ..< countOfRowsToInsert {
indexPathsToInsert.add(IndexPath(row: i, section: sectionClosed))
}
// 设计动画,以便让表格的打开和关闭拥有一个流畅(很屌)的效果
var insertAnimation: UITableViewRowAnimation
var deleteAnimation: UITableViewRowAnimation
insertAnimation = UITableViewRowAnimation.bottom
deleteAnimation = UITableViewRowAnimation.top
// 应用单元格的更新
self.tableView.beginUpdates()
self.tableView.insertRows(at: indexPathsToInsert as AnyObject as! [IndexPath], with: insertAnimation)
self.opensectionindex = sectionClosed
self.tableView.endUpdates()
}
}
func played() -> NSArray {
if playe == nil {
var base = BaseClass()
// let url = NSBundle.mainBundle().URLForResource("PlaysAndQuotations", withExtension: "plist")
// let playDictionariesArray = NSArray(contentsOfURL: url!)
//
var arr_name = base.cacheGetArray("Root")
var myName:AnyObject? = NSKeyedUnarchiver.unarchiveObject(with: arr_name as! Data) as AnyObject?
var playDictionariesArray = myName as? NSArray
var i = 0;
var number = 0;
//print("大家快来",playDictionariesArray)
playe = NSMutableArray(capacity: playDictionariesArray!.count)
for playDictionary in playDictionariesArray! {
let play: Play! = Play()
let result = playDictionary as? NSDictionary
play.name = result?["Playname"] as! String
print("测试name",play.name)
print("测试result",result)
let quotationDictionaries:NSArray = result?["quotations"] as! NSArray
number = number + quotationDictionaries.count
self.arr_number.append(number)
print("测试arr_result",self.arr_number)
let quotations = NSMutableArray(capacity: quotationDictionaries.count)
for quotationDictionary in quotationDictionaries {
let quotationDic:NSDictionary = quotationDictionary as! NSDictionary
let quotation: Quotation = Quotation()
quotation.setValuesForKeys(quotationDic as AnyObject as! [String : AnyObject])
quotations.add(quotation)
quotationDic[""]
i = i+1;
self.str_arr = "\(quotation.aname),\(quotation.bphone),\(quotation.ctell),\(i)"
self.arr_message.append(self.str_arr)
print("测试66666666",self.arr_message)
}
print("测试444444",quotations)
play.quotations = quotations
playe!.add(play)
}
var dd = self.arr_message.filter({ (ss) -> Bool in
return ss.contains("桂")
})
print("测试77777777777",dd)
}
return playe!
}
func usclick(_ barItem:UIBarButtonItem) {
print("您按了分享")
let alertController = UIAlertController(title: "关于我们",
message: "如果您有什么问题,请联系我们:[email protected]", preferredStyle: .alert)
let okAction = UIAlertAction(title: "确定", style: .default,
handler: {
action in
print("点击了确定")
})
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
func shareclick() {
print("您按了退出")
let alertController = UIAlertController(title: "系统提示",
message: "您确定要退出登录吗0.0?", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
let okAction = UIAlertAction(title: "确定", style: .default,
handler: {
action in
print("点击了确定")
var base = BaseClass()
let userDefault:UserDefaults = UserDefaults.standard
let ar:NSDictionary = userDefault.dictionaryRepresentation() as NSDictionary
for key in ar.allKeys {
// userDefault.removeObjectForKey(key as String)
//2015年5月2号修改
userDefault.removeObject(forKey: key as! String)
userDefault.synchronize()
}
let myStoryBoard = self.storyboard
let anotherView:UIViewController = (myStoryBoard?.instantiateViewController(withIdentifier: "get"))! as UIViewController
self.present(anotherView, animated: true, completion: nil)
})
alertController.addAction(cancelAction)
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
func warring() {
let alertController = UIAlertController(title: "系统提示",
message: "请检查您的ID输入是否正确或网络连接是否正常", preferredStyle: .alert)
let okAction = UIAlertAction(title: "好的", style: .default,
handler: {
action in
print("点击了确定")
var base = BaseClass()
let userDefault:UserDefaults = UserDefaults.standard
let ar:NSDictionary = userDefault.dictionaryRepresentation() as NSDictionary
for key in ar.allKeys {
// userDefault.removeObjectForKey(key as String)
//2015年5月2号修改
userDefault.removeObject(forKey: key as! String)
userDefault.synchronize()
}
let myStoryBoard = self.storyboard
let anotherView:UIViewController = (myStoryBoard?.instantiateViewController(withIdentifier: "get"))! as UIViewController
self.present(anotherView, animated: true, completion: nil)
})
alertController.addAction(okAction)
self.present(alertController, animated: true, completion: nil)
}
//搜索代理UISearchBarDelegate方法,每次改变搜索内容时都会调用
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
self.arr_arr = [[String]]()
if searchBar.text != "" {
for i in 0..<sectionInfoArray.count {
let sectionInfo:SectionInfo = self.sectionInfoArray[i] as! SectionInfo
sectionInfo.open = true
print("测试sectionInfoArray.count",sectionInfo.open)
}
}
else{
for i in 0..<sectionInfoArray.count {
let sectionInfo:SectionInfo = self.sectionInfoArray[i] as! SectionInfo
sectionInfo.open = false
}
print("测试sectionInfoArray.count2",sectionInfoArray.count)
}
self.search_text = searchBar.text!
self.filtered = self.arr_message.filter({ (ss) -> Bool in
return ss.contains(self.search_text)
})
var count:Int = 0
var a = 0
var final_count = 0
var zhong_count = 0
var sum_count = 0
self.arr_rows = [Int]()
for i in 0..<self.arr_number.count{
for k in 0..<self.filtered.count{
var str = self.filtered[k].components(separatedBy: ",")
var number:Int = Int(str[3])!
if a <= k {
if number < arr_number[i]+1{
self.arr_str.append(self.filtered[a])
a = a + 1
}
}
if number < arr_number[i]+1{
count = count + 1
}
}
self.arr_arr.append(self.arr_str)
self.arr_str = [String]()
print("测试arr_arr",arr_arr)
final_count = count - sum_count - zhong_count
zhong_count = count
sum_count = sum_count + final_count
self.arr_rows.append(final_count)
print("测试arr_rows",self.arr_rows)
}
print(self.search_text)
print("测试filtered",self.filtered)
self.tableView.reloadData()
}
func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
searchBar.resignFirstResponder()
}
func handleTap(_ sender: UITapGestureRecognizer) {
if sender.state == .ended {
print("收回键盘")
searchBar.resignFirstResponder()
}
sender.cancelsTouchesInView = false
}
func replace() {
present(selectorController, animated: true, completion: nil)
}
}
//MARK: 扩展图片选择和结果返回
extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
// MARK: 图片选择器界面
// MARK: 用于弹出选择的对话框界面
var selectorController: UIAlertController {
let controller = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
controller.addAction(UIAlertAction(title: "取消", style: .cancel, handler: nil)) // 取消按钮
controller.addAction(UIAlertAction(title: "拍照选择", style: .default) { action in
self.selectorSourceType(.camera)
}) // 拍照选择
controller.addAction(UIAlertAction(title: "相册选择", style: .default) { action in
self.selectorSourceType_2(.photoLibrary)
}) // 相册选择
return controller
}
func selectorSourceType(_ type: UIImagePickerControllerSourceType) {
imagePickerController.sourceType = type
imagePickerController.delegate = self
// 打开图片选择器
present(imagePickerController, animated: true, completion: nil)
}
func selectorSourceType_2(_ type: UIImagePickerControllerSourceType) {
imagePickerController_2.sourceType = type
imagePickerController_2.delegate = self
// 打开图片选择器
present(imagePickerController_2, animated: true, completion: nil)
}
// MARK: 当图片选择器选择了一张图片之后回调
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) {
dismiss(animated: true, completion: nil) // 选中图片, 关闭选择器...这里你也可以 picker.dismissViewControllerAnimated 这样调用...但是效果都是一样的...
// Image_tou.image = info[UIImagePickerControllerOriginalImage] as? UIImage // 显示图片
// Image_tou.contentMode = .scaleToFill // 缩放显示, 便于查看全部的图片
self.tb_bgname = info[UIImagePickerControllerOriginalImage] as? UIImage // 显示图片
var base = BaseClass()
var Data_array:Data = NSKeyedArchiver.archivedData(withRootObject: info[UIImagePickerControllerOriginalImage])
base.cacheSetArray("bgname", value: Data_array as AnyObject)
let tb_img = UIImageView(image: self.tb_bgname)
self.tableView.backgroundView = tb_img
}
// MARK: 当点击图片选择器中的取消按钮时回调
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil) // 效果一样的...
}
}