123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- //
- // PDFSignatureManager.m
- // PDFReader
- //
- // Copyright © 2014-2022 PDF Technologies, Inc. All Rights Reserved.
- //
- // The PDF Reader Sample applications are licensed with a modified BSD license.
- // Please see License for details. This notice may not be removed from this file.
- //
- #import "PDFSignatureManager.h"
- #define kPDFSignatureDataFileName (@"PDFKitResources/Signature/PDFSignatureData.plist")
- #define kPDFSignatureImageFolder (@"PDFKitResources/Signature/PDFSignatureImageFolder")
- @implementation PDFSignatureManager
- static PDFSignatureManager *_sharedSignManager;
- + (PDFSignatureManager *)sharedManager {
- if (!_sharedSignManager)
- _sharedSignManager = [[PDFSignatureManager alloc] init];
- return _sharedSignManager;
- }
- - (instancetype)init {
- if (self = [super init]) {
- NSString *library = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
- NSString *filePath = [library stringByAppendingPathComponent:kPDFSignatureDataFileName];
- NSString *folderPath = [library stringByAppendingPathComponent:kPDFSignatureImageFolder];
- if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
- NSMutableArray *signatures = [NSMutableArray array];
- NSArray *fileNames = [[[NSArray alloc] initWithContentsOfFile:filePath] autorelease];
- for (NSString *fileName in fileNames) {
- [signatures addObject:[folderPath stringByAppendingPathComponent:fileName]];
- }
- _signatures = [signatures retain];
- } else {
- _signatures = [[NSArray alloc] init];
- }
- }
- return self;
- }
- - (void)dealloc {
- [_signatures release];
- [super dealloc];
- }
- - (NSString *)randomString {
- CFUUIDRef cfuuid = CFUUIDCreate(kCFAllocatorDefault);
- CFStringRef cfstring = CFUUIDCreateString(kCFAllocatorDefault, cfuuid);
- NSString *cfuuidString = [NSString stringWithString:(__bridge NSString *)cfstring];
- CFRelease(cfuuid);
- CFRelease(cfstring);
- return cfuuidString;
- }
- - (void)save {
- NSString *library = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
- NSString *filePath = [library stringByAppendingPathComponent:kPDFSignatureDataFileName];
- if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
- [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
- }
- NSMutableArray *fileNames = [NSMutableArray array];
- for (NSString *filePath in self.signatures) {
- [fileNames addObject:filePath.lastPathComponent];
- }
- if (fileNames.count > 0) {
- [fileNames writeToFile:filePath atomically:YES];
- }
- }
- - (void)addSignature:(UIImage *)image {
- NSString *library = [NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
- NSString *folderPath = [library stringByAppendingPathComponent:kPDFSignatureImageFolder];
- NSString *randomStr = [self randomString];
- if (image) {
- if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath]) {
- [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
- }
- NSString *imageName = [randomStr stringByAppendingPathExtension:@"png"];
- NSString *imagePath = [folderPath stringByAppendingPathComponent:imageName];
- if ([UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES]) {
- NSMutableArray *array = [NSMutableArray arrayWithArray:self.signatures];
- [array addObject:imagePath];
- self.signatures = array;
- [self save];
- }
- }
- }
- - (void)removeSignaturesAtIndexes:(NSIndexSet *)indexes {
- for (NSString *filePath in [self.signatures objectsAtIndexes:indexes]) {
- [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
- }
- NSMutableArray *array = [NSMutableArray arrayWithArray:self.signatures];
- [array removeObjectsAtIndexes:indexes];
- self.signatures = array;
- [self save];
- }
- @end
|