forked from PredixDev/MobileExample-iOSService-Camera
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPMCamera.swift
More file actions
898 lines (728 loc) · 25.5 KB
/
PMCamera.swift
File metadata and controls
898 lines (728 loc) · 25.5 KB
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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
// PMCamera.swift
//
// PredixMobileReferenceApp
//
// Created by on 2/29/16.
// Copyright © 2016 GE. All rights reserved.
//
import Foundation
import UIKit
import MobileCoreServices
import PredixMobileSDK
//MARK: PMCamera
/**
Responsible for camera access and scanning barcode
*/
class PMCamera: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIPopoverControllerDelegate
{
/// a shared instance
static let sharedInstance = PMCamera()
typealias barcodeReturnBlock = (NSData?) -> Void
typealias errorReturnBlock = (NSData?) -> Void
private var mErrorReturn:errorReturnBlock?
private var mBarcodeReturn:barcodeReturnBlock?
var picker:UIImagePickerController?
weak var imageView: UIImageView!
var popover:UIPopoverController?=nil
private var responseData :[String : AnyObject]?
private var topViewController: UIViewController?
private var mOptions :[String : AnyObject]?
private var state:State? = State.IDLE
private var photoDirPath:String?
private var videoDirPath:String?
private enum ResponseType : Int {
case Photo_src
case Photo_location
case Video_src
case Video_location
case msg
case error
}
private enum LocationType : Int {
case Data
case Location
case Native
}
private enum State : Int {
case IDLE
case PROCESSING
}
// private enum xxx : Int
// {
// case Photo
// case Video
// }
override init() {
self.picker=UIImagePickerController()
do
{
// try PMCamera.listFiles(PMCamera.imageDirName)
try photoDirPath = PMCamera.createDir(PMCamera.photoDirName)
try videoDirPath = PMCamera.createDir(PMCamera.videoDirName)
}
catch let error as NSError
{
print("an error during PMCamera initialization:- \(error.localizedDescription)")
}
}
/**
TODO
- Parameter errorReturnBlock : (NSData?) -> Void
- Parameter barcodeReturnBlock : (NSData?) -> Void
- Returns: :-)
*/
func processPOSTRequest(options : Dictionary<String, AnyObject>, errorReturn : errorReturnBlock, barcodeReturn : barcodeReturnBlock)
{
guard self.state == State.IDLE else
{
sendBusyResponse(errorReturn)
return
}
self.state = State.PROCESSING
self.mErrorReturn = errorReturn
self.mBarcodeReturn = barcodeReturn
self.mOptions = options
picker?.delegate = self
picker!.allowsEditing = self.getEditingAllowed()
switch(self.getSourceType()) {
case UIImagePickerControllerSourceType.Camera:
// self.picker!.sourceType = UIImagePickerControllerSourceType.Camera
let mediaType = self.getMediaType()
if(1 == mediaType) //its video
{
picker!.mediaTypes = [/*kUTTypeImage as String,*/ kUTTypeMovie as String, kUTTypeVideo as String]
}
openCamera()
case UIImagePickerControllerSourceType.PhotoLibrary:
self.picker!.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
picker!.mediaTypes = [kUTTypeImage as String, kUTTypeMovie as String, kUTTypeVideo as String]
openGallary()
case UIImagePickerControllerSourceType.SavedPhotosAlbum:
self.picker!.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
picker!.mediaTypes = [kUTTypeImage as String, kUTTypeMovie as String, kUTTypeVideo as String]
openGallary()
// default:
// self.sendResponse("Unknown source type option!", type: .error)
}
}
/**
Handels GET request
- Parameter errorReturnBlock : (NSData?) -> Void
- Parameter barcodeReturnBlock : (NSData?) -> Void
- Returns: :-)
*/
func processGETRequest(entity : String, errorReturn : errorReturnBlock, barcodeReturn : barcodeReturnBlock)
{
guard self.state == State.IDLE else
{
sendBusyResponse(errorReturn)
return
}
self.state = State.PROCESSING
self.mErrorReturn = errorReturn
self.mBarcodeReturn = barcodeReturn
switch (entity)
{
case "image":
do
{
let files = try PMCamera.getFileURLs(.Photo_location)
self.sendResponse(convert(files), type: .Photo_location)
}
catch PMCameraError.FileHandlingError(let errorMsg)
{
self.sendResponse("\(errorMsg)", type: .error)
}
catch PMCameraError.MaxCacheError(let errorMsg)
{
self.sendResponse("\(errorMsg)", type: .error)
}
catch let error as NSError
{
self.sendResponse("\(error.localizedDescription)", type: .error)
}
case "video":
do
{
let files = try PMCamera.getFileURLs(.Video_location)
self.sendResponse(convert(files), type: .Video_location)
}
catch PMCameraError.FileHandlingError(let errorMsg)
{
self.sendResponse("\(errorMsg)", type: .error)
}
catch PMCameraError.MaxCacheError(let errorMsg)
{
self.sendResponse("\(errorMsg)", type: .error)
}
catch let error as NSError
{
self.sendResponse("\(error.localizedDescription)", type: .error)
}
default:
self.sendResponse("Unknown source type option!", type: .error)
}
}
/**
Handels GET request
- Parameter errorReturnBlock : (NSData?) -> Void
- Parameter barcodeReturnBlock : (NSData?) -> Void
- Returns: :-)
*/
func processDeleteRequest(entity : String, errorReturn : errorReturnBlock, barcodeReturn : barcodeReturnBlock)
{
guard self.state == State.IDLE else
{
sendBusyResponse(errorReturn)
return
}
self.state = State.PROCESSING
self.mErrorReturn = errorReturn
self.mBarcodeReturn = barcodeReturn
switch (entity)
{
case "image":
do
{
try PMCamera.deleteFiles(.Photo_location)
self.sendResponse("Files deleted successfully", type: .msg)
}catch PMCameraError.FileHandlingError(let errorMsg)
{
self.sendResponse("\(errorMsg)", type: .error)
}
catch PMCameraError.MaxCacheError(let errorMsg)
{
self.sendResponse("\(errorMsg)", type: .error)
}
catch let error as NSError
{
self.sendResponse("\(error.localizedDescription)", type: .error)
}
case "video":
do
{
try PMCamera.deleteFiles(.Video_location)
self.sendResponse("Files deleted successfully", type: .msg)
}catch PMCameraError.FileHandlingError(let errorMsg)
{
self.sendResponse("\(errorMsg)", type: .error)
}
catch PMCameraError.MaxCacheError(let errorMsg)
{
self.sendResponse("\(errorMsg)", type: .error)
}
catch let error as NSError
{
self.sendResponse("\(error.localizedDescription)", type: .error)
}
default: //TODO: functionality to delete a single file
self.sendResponse("Unknown source type option in file delete!", type: .error)
}
}
// MARK: private functions
/**
Converts [NSURL] to String
- Parameter filesPath: an array containing files path
- Returns: String
*/
private func convert(filesPath: [NSURL]) -> String
{
var toReturn:String = ""
if filesPath.count < 1
{
toReturn = "No files yet..."
}
for path : NSURL in filesPath {
toReturn = toReturn + path.absoluteString + ","
}
return toReturn
}
/**
Top View controller
- Returns: UIViewController
*/
private func getTopVC() ->UIViewController
{
var toReturn:UIViewController?
if let topController = UIApplication.sharedApplication().keyWindow?.rootViewController {
if(topController.presentedViewController == nil)
{
toReturn = topController
}
else
{
while let presentedViewController = topController.presentedViewController {
toReturn = presentedViewController
break
}
}
}
return toReturn!
}
/**
Busy error response
- Parameter errorReturn: errorReturnBlock
*/
private func sendBusyResponse(errorReturn : errorReturnBlock)
{
var busyRespData :[String : AnyObject]? = [String : AnyObject]()
busyRespData!["error".lowercaseString] = "Already processing a request, Please try again."
do
{
let toReturn = try NSJSONSerialization.dataWithJSONObject(busyRespData!, options: NSJSONWritingOptions(rawValue: 0))
errorReturn(toReturn)
}
catch let error
{
PGSDKLogger.error("Error serializing busy response data into JSON: \(error)")
}
}
/**
returns a response to calling closure
- Parameter msg: message which will be sent back
- Parameter type: type of message
*/
private func sendResponse(msg: String, type: ResponseType)
{
self.responseData = [String : AnyObject]()
self.state = State.IDLE
switch(type)
{
case .Photo_src:
self.responseData!["image_src".lowercaseString] = msg
case .Photo_location:
self.responseData!["image_location".lowercaseString] = msg
case .Video_src:
self.responseData!["video_src".lowercaseString] = msg
case .Video_location:
self.responseData!["video_location".lowercaseString] = msg
case .msg:
self.responseData!["message".lowercaseString] = msg
case .error:
self.responseData!["error".lowercaseString] = msg
}
do
{
let toReturn = try NSJSONSerialization.dataWithJSONObject(self.responseData!, options: NSJSONWritingOptions(rawValue: 0))
switch(type)
{
case .error:
self.mErrorReturn!(toReturn)
case .Photo_src, .Photo_location, .Video_src, .Video_location, .msg:
self.mBarcodeReturn!(toReturn)
}
}
catch let error
{
PGSDKLogger.error("Error serializing user data into JSON: \(error)")
}
}
func openCamera()
{
if(UIImagePickerController .isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera))
{
self.picker!.sourceType = UIImagePickerControllerSourceType.Camera
self.topViewController = getTopVC()
dispatch_async(dispatch_get_main_queue()) {
self.topViewController!.presentViewController(self.picker!, animated: true, completion: nil)
}
}
else
{
PGSDKLogger.error("Camera not accessible!")
self.sendResponse("Camera not accessible", type: .error)
}
}
func openGallary()
{
if UIDevice.currentDevice().userInterfaceIdiom == .Phone
{
self.topViewController = getTopVC()
dispatch_async(dispatch_get_main_queue()) {
self.topViewController!.presentViewController(self.picker!, animated: true, completion: nil)
}
}
else
{
self.topViewController = getTopVC()
dispatch_async(dispatch_get_main_queue()) {
self.popover=UIPopoverController(contentViewController: self.picker!)
self.popover!.presentPopoverFromRect(CGRectMake(50.0, 50.0, 400, 400), inView: self.topViewController!.view, permittedArrowDirections: UIPopoverArrowDirection.Any, animated: true)
}
}
}
// MARK: image picker delegate
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{
picker .dismissViewControllerAnimated(true, completion: nil)
var outputData:NSData?
let mediaType = info[UIImagePickerControllerMediaType] as! NSString
// Handle a movie capture
if mediaType == kUTTypeMovie || mediaType == kUTTypeVideo
{
let path = (info[UIImagePickerControllerMediaURL] as! NSURL)
outputData = NSData(contentsOfURL: path)!
self.mOptions!["mediaType"] = 1 // Overriding options based on what we received from Picker
}
else
{
self.mOptions!["mediaType"] = 0 // Overriding options based on what we received from Picker
let returnImg = info[UIImagePickerControllerOriginalImage] as? UIImage
outputData = UIImageJPEGRepresentation(returnImg!, self.getCompression())
}
generateOutput(outputData!)
}
func imagePickerControllerDidCancel(picker: UIImagePickerController)
{
self.picker!.dismissViewControllerAnimated(true, completion: nil)
self.sendResponse("User cancelled", type: .error)
}
private func generateOutput(data: NSData)
{
switch(getOutputType())
{
case .Photo_src:
print("send base64")
let base64String = data.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
self.sendResponse(base64String, type: .Photo_src)
case .Photo_location:
saveNSendResponse(data, type: .Photo_location)
case .Video_location:
saveNSendResponse(data, type: .Video_location)
case .error, .msg, .Video_src:
self.sendResponse("Unknown output format! 1", type: .error)
}
}
// MARK: Options parser - Seperate Class^
/**
Compression option
- Returns : CGFloat
*/
private func getCompression() ->CGFloat
{
var toReturn:CGFloat
toReturn = 0.0
if let compression = self.mOptions!["compression"] as? CGFloat
{
toReturn = compression/100
}
return toReturn
}
/**
Source type option
*/
private func getSourceType() ->UIImagePickerControllerSourceType
{
var toReturn:UIImagePickerControllerSourceType
toReturn = UIImagePickerControllerSourceType.PhotoLibrary
if let srcType = self.mOptions!["sourceType"] as? Int
{
toReturn = UIImagePickerControllerSourceType(rawValue: srcType)!
}
return toReturn
}
/**
Media option
*/
private func getMediaType() ->Int
{
var toReturn:Int
toReturn = 0
if let srcType = self.mOptions!["mediaType"] as? Int
{
toReturn = srcType
}
return toReturn
}
/**
Output option
*/
private func getOutputType() ->ResponseType
{
var toReturn:ResponseType
toReturn = .error
if let outputType = self.mOptions!["output"] as? Int
{
// toReturn = outputType ==
switch(outputType) {
case 0: //File URI
toReturn = (0 == self.getMediaType()) ? .Photo_location : .Video_location
case 1: // SRC
toReturn = (0 == self.getMediaType()) ? .Photo_src : .error
default:
toReturn = .error
}
}
return toReturn
}
/**
Editing option
- Returns: Bool
*/
private func getEditingAllowed() ->Bool
{
var toReturn:Bool
toReturn = false
if let edit = self.mOptions!["edit"] as? Int
{
toReturn = (edit == 0) ? false : true
}
return toReturn
}
// TODO:
private func getCameraDirection() ->Int
{
var toReturn:Int
toReturn = 0
if let cameraDirection = self.mOptions!["cameraDirection"] as? Int
{
toReturn = cameraDirection
}
return toReturn
}
// MARK: File handling - Seperate Class^
static let FILE_NAME_SUFFIX = "PM_CAMERA_"
static let photoDirName = "image"
static let videoDirName = "video"
static let maxImgFiles = 100
static let maxMovFiles = 20
enum PMCameraError : ErrorType {
case FileHandlingError(String)
case MaxCacheError(String)
}
/// Saves data in relevant format based on provided options and returns a response
///
/// - Parameters:
/// - dataToBeWritten: image or video data which needs to be persisted
/// - type: type of response
private func saveNSendResponse(dataToBeWritten: NSData, type: ResponseType)
{
do
{
let savePath = try generateUniqueFilename(type)
let isSaved = dataToBeWritten.writeToFile(savePath, atomically: true)
if isSaved
{
switch(type)
{
case .Photo_location:
self.sendResponse(savePath, type: .Photo_location)
case .Video_location:
self.sendResponse(savePath, type: .Video_location)
case .Photo_src, .Video_src, .msg, .error:
self.sendResponse("Unknown output format! 2", type: .error)
}
}
else
{
self.sendResponse("Error in saving file!", type: .error)
}
}
catch PMCameraError.FileHandlingError(let errorMsg)
{
self.sendResponse("\(errorMsg)", type: .error)
}
catch PMCameraError.MaxCacheError(let errorMsg)
{
self.sendResponse("\(errorMsg)", type: .error)
}
catch let error as NSError
{
self.sendResponse("\(error.localizedDescription)", type: .error)
}
}
/**
Generates a unique name for image/mov types
- Parameter type : Photo_location / Video_location
- Returns: filename as String
*/
private func generateUniqueFilename(type: ResponseType) throws -> String {
do
{
if try PMCamera.isFileCacheLimitReached(type)
{
throw PMCameraError .MaxCacheError("Max limit of storage reached. Try deleting few files first.")
}
}
catch let error as NSError
{
throw error
}
var extensionName:String?
var documentPath:String?
switch(type)
{
case .Photo_location:
extensionName = "jpeg"
documentPath = photoDirPath
case .Video_location:
extensionName = "mov"
documentPath = videoDirPath
case .Photo_src, .Video_src, .msg, .error:
throw PMCameraError .FileHandlingError("Invalid option in file generation!!")
// extensionName = "ERROR_FORMAT"
// documentPath = "ERROR_FORMAT"
}
let guid = NSProcessInfo.processInfo().globallyUniqueString
let uniqueFileName = documentPath! + "/" + ("\(PMCamera.FILE_NAME_SUFFIX)\(guid).\(extensionName!)")
print("uniqueFileName: \(uniqueFileName)")
// self.listFiles(imageDirName)
return uniqueFileName
}
/**
Creates a directiory in PMCamera specific folder
- Parameter dirName : directory name
- Returns: path to directory as String
*/
private static func createDir(dirName: String) throws -> String
{
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
let documentsDirectory: AnyObject = paths[0]
let dataPath = documentsDirectory.stringByAppendingPathComponent(dirName)
var isDir : ObjCBool = false
if NSFileManager.defaultManager().fileExistsAtPath(dataPath, isDirectory: &isDir) {
if isDir {
// file exists & its a dir :-)
} else {
// file exists & isn't dir??? whoa- TODO:? :-(
}
}
else {
// file doesn't exist :-<
do {
try NSFileManager.defaultManager().createDirectoryAtPath(dataPath, withIntermediateDirectories: false, attributes: nil)
} catch let error as NSError {
print(error.localizedDescription)
throw error
}
}
return dataPath
}
/**
Crawls through all files in PMCamera
- Parameter type : Imahe or Mov dir
- Returns: ^
*/
private static func isFileCacheLimitReached(type: ResponseType) throws -> Bool
{
return false
var dirName:String?
switch(type)
{
case .Photo_location:
dirName = photoDirName
case .Video_location:
dirName = videoDirName
case .Photo_src, .Video_src, .msg, .error:
throw PMCameraError.FileHandlingError("Invalid option in response type!")
}
var toReturn:Bool = false
do
{
let files = try PMCamera.getFileURLs(type)
if dirName == photoDirName && files.count >= maxImgFiles
{
toReturn = true
}
else if dirName == videoDirName && files.count >= maxMovFiles
{
toReturn = true
}
}
catch let error as NSError
{
throw error
}
return toReturn
}
/**
Crawls through all files in PMCamera
- Parameter type : Imahe or Mov dir
- Returns: ^
*/
private static func getFileURLs(type: ResponseType) throws -> [NSURL]
{
var dirName:String?
switch(type)
{
case .Photo_location:
dirName = photoDirName
case .Video_location:
dirName = videoDirName
case .Photo_src, .Video_src, .msg, .error:
throw PMCameraError.FileHandlingError("Invalid option in response type!")
}
var toReturn:[NSURL]
let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first!
var dataPath:NSURL
if #available(iOS 9.0, *) {
dataPath = NSURL(fileURLWithPath: dirName!, isDirectory: true, relativeToURL: documentsUrl)
} else {
dataPath = documentsUrl.URLByAppendingPathComponent(dirName!, isDirectory: true)//NSURL(fileURLWithPath: dirName, isDirectory: true)
}
// print("documentsUrl: \(documentsUrl)")
do {
toReturn = try NSFileManager.defaultManager().contentsOfDirectoryAtURL(dataPath, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions())
} catch let error as NSError {
print(error.localizedDescription)
throw error
}
return toReturn
}
/**
Deletes a given file from a PMCamera specific dir
- Parameter name : file name with extension
- Returns: ^
*/
private static func deleteFiles(type: ResponseType) throws
{
do
{
// TEST
// try PMCamera.deleteFile("someName")
let files:[NSURL] = try PMCamera.getFileURLs(type)
if files.count < 1
{
throw PMCameraError.FileHandlingError("No file cached!")
}
for path : NSURL in files {
let slicedPath:String = path.absoluteString.stringByReplacingOccurrencesOfString("file://", withString: "")
if NSFileManager.defaultManager().fileExistsAtPath(slicedPath)
{
try NSFileManager.defaultManager().removeItemAtPath(slicedPath)
print("old photo has been removed")
}
}
}
// catch PMCameraError.FileHandlingError(let errorMsg)
// {
// throw errorMsg
// }
// catch PMCameraError.MaxCacheError(let errorMsg)
// {
// throw errorMsg
// }
catch let error as NSError
{
throw error
}
}
private static func deleteFile(name:String) throws
{
let paths = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)
if paths.count > 0 {
let dirPath = paths[0]
let fileName = "someFileName"
let filePath = NSString(format:"%@/%@.png", dirPath, fileName) as String
if NSFileManager.defaultManager().fileExistsAtPath(filePath) {
do {
try NSFileManager.defaultManager().removeItemAtPath(filePath)
print("old photo has been removed")
} catch let error as NSError{
print("an error during a removing:- \(error.localizedDescription)")
throw error
}
}
}
}
}