PDFSignatureManager.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // PDFSignatureManager.m
  3. // PDFReader
  4. //
  5. // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
  6. //
  7. // The PDF Reader Sample applications are licensed with a modified BSD license.
  8. // Please see License for details. This notice may not be removed from this file.
  9. //
  10. #import "PDFSignatureManager.h"
  11. #define kPDFSignatureDataFileName (@"PDFKitResources/Signature/PDFSignatureData.plist")
  12. #define kPDFSignatureImageFolder (@"PDFKitResources/Signature/PDFSignatureImageFolder")
  13. @implementation PDFSignatureManager
  14. static PDFSignatureManager *_sharedSignManager;
  15. + (PDFSignatureManager *)sharedManager {
  16. if (!_sharedSignManager)
  17. _sharedSignManager = [[PDFSignatureManager alloc] init];
  18. return _sharedSignManager;
  19. }
  20. - (instancetype)init {
  21. if (self = [super init]) {
  22. NSString *library = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
  23. NSString *filePath = [library stringByAppendingPathComponent:kPDFSignatureDataFileName];
  24. NSString *folderPath = [library stringByAppendingPathComponent:kPDFSignatureImageFolder];
  25. if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
  26. NSMutableArray *signatures = [NSMutableArray array];
  27. NSArray *fileNames = [[[NSArray alloc] initWithContentsOfFile:filePath] autorelease];
  28. for (NSString *fileName in fileNames) {
  29. [signatures addObject:[folderPath stringByAppendingPathComponent:fileName]];
  30. }
  31. _signatures = [signatures retain];
  32. } else {
  33. _signatures = [[NSArray alloc] init];
  34. }
  35. }
  36. return self;
  37. }
  38. - (void)dealloc {
  39. [_signatures release];
  40. [super dealloc];
  41. }
  42. - (NSString *)randomString {
  43. CFUUIDRef cfuuid = CFUUIDCreate(kCFAllocatorDefault);
  44. CFStringRef cfstring = CFUUIDCreateString(kCFAllocatorDefault, cfuuid);
  45. NSString *cfuuidString = [NSString stringWithString:(__bridge NSString *)cfstring];
  46. CFRelease(cfuuid);
  47. CFRelease(cfstring);
  48. return cfuuidString;
  49. }
  50. - (void)save {
  51. NSString *library = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
  52. NSString *filePath = [library stringByAppendingPathComponent:kPDFSignatureDataFileName];
  53. if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
  54. [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
  55. }
  56. NSMutableArray *fileNames = [NSMutableArray array];
  57. for (NSString *filePath in self.signatures) {
  58. [fileNames addObject:filePath.lastPathComponent];
  59. }
  60. if (fileNames.count > 0) {
  61. [fileNames writeToFile:filePath atomically:YES];
  62. }
  63. }
  64. - (void)addSignature:(UIImage *)image {
  65. NSString *library = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
  66. NSString *folderPath = [library stringByAppendingPathComponent:kPDFSignatureImageFolder];
  67. NSString *randomStr = [self randomString];
  68. if (image) {
  69. if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath]) {
  70. [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
  71. }
  72. NSString *imageName = [randomStr stringByAppendingPathExtension:@"png"];
  73. NSString *imagePath = [folderPath stringByAppendingPathComponent:imageName];
  74. if ([UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES]) {
  75. NSMutableArray *array = [NSMutableArray arrayWithArray:self.signatures];
  76. [array addObject:imagePath];
  77. self.signatures = array;
  78. [self save];
  79. }
  80. }
  81. }
  82. - (void)removeSignaturesAtIndexes:(NSIndexSet *)indexes {
  83. for (NSString *filePath in [self.signatures objectsAtIndexes:indexes]) {
  84. [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
  85. }
  86. NSMutableArray *array = [NSMutableArray arrayWithArray:self.signatures];
  87. [array removeObjectsAtIndexes:indexes];
  88. self.signatures = array;
  89. [self save];
  90. }
  91. @end