|
@@ -1,644 +0,0 @@
|
|
|
-//
|
|
|
-// KMStampManager.m
|
|
|
-// SignFlow Premium
|
|
|
-//
|
|
|
-// Created by wangshuai on 14/12/1.
|
|
|
-// Copyright (c) 2014年 zhangjie. All rights reserved.
|
|
|
-//
|
|
|
-
|
|
|
-#import "KMStampManager.h"
|
|
|
-
|
|
|
-#define kmApplicationSupportDirectory [[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier]
|
|
|
-#define kStampFolderPath [kmApplicationSupportDirectory stringByAppendingPathComponent:@"COfficeStamp"]
|
|
|
-#define kStampPlistPath [[kmApplicationSupportDirectory stringByAppendingPathComponent:@"COfficeStamp"] stringByAppendingPathComponent:@"COfficeStamp.plist"]
|
|
|
-
|
|
|
-#define kStampOfStringKey @"StampStringKey"
|
|
|
-#define kStampOfSubStringKey @"StampSubStringKey"
|
|
|
-#define kStampOfColorKey @"StampColorKey"
|
|
|
-#define kStampOfImagePathKey @"StampImagePathKey"
|
|
|
-#define kStampOfDateKey @"kStampOfDateKey"
|
|
|
-#define kStampOfCategoryKey @"kStampOfCategoryKey"
|
|
|
-#define kStampOfNameKey @"kStampOfNameKey"
|
|
|
-#define kStampShapeKey @"kStampShapeKey"
|
|
|
-#define kStampDateKey @"kStampDateKey"
|
|
|
-#define kStampTimeKey @"kStampTimeKey"
|
|
|
-#define kStampNeedExchangKey @"kStampNeedExchangKey"
|
|
|
-
|
|
|
-@implementation KMStampManager
|
|
|
-
|
|
|
-- (NSString *)tagString {
|
|
|
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
|
|
|
- dateFormatter.dateFormat = @"yyMMddHHmmss";
|
|
|
- return [NSString stringWithFormat:@"%@%04d", [dateFormatter stringFromDate:[NSDate date]], rand()%10000];
|
|
|
-}
|
|
|
-
|
|
|
-+ (KMStampManager *)defaultManager
|
|
|
-{
|
|
|
- static KMStampManager *singleton = nil;
|
|
|
- static dispatch_once_t pred;
|
|
|
- dispatch_once(&pred, ^{
|
|
|
- singleton = [[KMStampManager alloc] init];
|
|
|
- });
|
|
|
- return singleton;
|
|
|
-}
|
|
|
-
|
|
|
-- (id)init
|
|
|
-{
|
|
|
- if (self = [super init]) {
|
|
|
- _stamps = [[NSMutableArray alloc] init];
|
|
|
- [_stamps addObjectsFromArray:[self defaultStandardStamps]];
|
|
|
- [_stamps addObjectsFromArray:[self dynamicStamps]];
|
|
|
- if ([[NSFileManager defaultManager] fileExistsAtPath:kStampPlistPath]) {
|
|
|
- NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:kStampPlistPath];
|
|
|
- NSMutableArray *deleteKeys = [NSMutableArray array];
|
|
|
- for (NSString *key in [dictionary allKeys]) {
|
|
|
- NSDictionary *dic = [dictionary objectForKey:key];
|
|
|
-
|
|
|
- BOOL isAdd = YES;
|
|
|
- NSString *text = @"";
|
|
|
- NSString *dateString = @"";
|
|
|
- NSInteger color = 0;
|
|
|
- NSString *date = @"";
|
|
|
- NSInteger categorytype = 0;
|
|
|
- NSString *nameString = @"";
|
|
|
- NSString *imagePath = @"";
|
|
|
- NSInteger stampShape = 0;
|
|
|
- BOOL needChange = NO;
|
|
|
- if ([dic objectForKey:kStampOfStringKey]) {
|
|
|
- text = [dic objectForKey:kStampOfStringKey];
|
|
|
- }
|
|
|
- if ([dic objectForKey:kStampOfSubStringKey]) {
|
|
|
- dateString = [dic objectForKey:kStampOfSubStringKey];
|
|
|
- }
|
|
|
- if ([dic objectForKey:kStampOfColorKey]) {
|
|
|
- color = [[dic objectForKey:kStampOfColorKey] integerValue];
|
|
|
- }
|
|
|
- if ([dic objectForKey:kStampOfDateKey]) {
|
|
|
- date = [dic objectForKey:kStampOfDateKey];
|
|
|
- }
|
|
|
-
|
|
|
- if ([dic objectForKey:kStampOfCategoryKey]) {
|
|
|
- categorytype = [[dic objectForKey:kStampOfCategoryKey] integerValue];
|
|
|
- }
|
|
|
-
|
|
|
- if ([dic objectForKey:kStampOfNameKey]) {
|
|
|
- nameString = [dic objectForKey:kStampOfNameKey];
|
|
|
- }
|
|
|
-
|
|
|
- if ([dic objectForKey:kStampShapeKey]) {
|
|
|
- stampShape = [[dic objectForKey:kStampShapeKey] integerValue];
|
|
|
- }
|
|
|
-
|
|
|
- if ([dic objectForKey:kStampNeedExchangKey]) {
|
|
|
- needChange = [[dic objectForKey:kStampNeedExchangKey] boolValue];
|
|
|
- }
|
|
|
-
|
|
|
- if ([dic objectForKey:kStampOfImagePathKey]) {
|
|
|
- NSString *path = [kStampFolderPath stringByAppendingPathComponent:[dic objectForKey:kStampOfImagePathKey]];
|
|
|
- if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {
|
|
|
- imagePath = path;
|
|
|
- } else {
|
|
|
- isAdd = NO;
|
|
|
- [deleteKeys addObject:key];
|
|
|
- }
|
|
|
- }
|
|
|
- CStampObject *stamp = nil;
|
|
|
- if(imagePath.length > 0 && categorytype == 2) {
|
|
|
- stamp = [[CStampObject alloc] initWithImageStampFilePath:imagePath];
|
|
|
- } else {
|
|
|
- if (categorytype == 4) {
|
|
|
- isAdd = NO;
|
|
|
- } else if (categorytype == 1) {
|
|
|
- NSString *dateS = @"";
|
|
|
- NSString *time = @"";
|
|
|
- if ([dic objectForKey:kStampDateKey]) {
|
|
|
- dateS = [dic objectForKey:kStampDateKey];
|
|
|
- }
|
|
|
- if ([dic objectForKey:kStampTimeKey]) {
|
|
|
- time = [dic objectForKey:kStampTimeKey];
|
|
|
- }
|
|
|
- stamp = [[CStampObject alloc] initWithText:text detailText:dateString style:color shape:stampShape];
|
|
|
- stamp.dateString = dateS;
|
|
|
- stamp.timeString = time;
|
|
|
- }
|
|
|
- }
|
|
|
- stamp.stampKey = key;
|
|
|
- if (isAdd) {
|
|
|
- [self.stamps addObject:stamp];
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (deleteKeys.count > 0) {
|
|
|
- NSMutableDictionary *newDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionary];
|
|
|
- for (NSString *key in deleteKeys) {
|
|
|
- [newDictionary removeObjectForKey:key];
|
|
|
- }
|
|
|
- [newDictionary writeToFile:kStampPlistPath atomically:YES];
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return self;
|
|
|
-}
|
|
|
-
|
|
|
-#pragma mark - Default Data
|
|
|
-
|
|
|
-- (NSMutableArray *)defaultStandardStamps {
|
|
|
- NSMutableArray *dynamic = [[NSMutableArray alloc] init];
|
|
|
- NSArray *standarArray = @[@(CStandardStampTypeApproved),
|
|
|
- @(CStandardStampTypeNotapproved),
|
|
|
- @(CStandardStampTypeDraft),
|
|
|
- @(CStandardStampTypeFinal),
|
|
|
- @(CStandardStampTypeCompleted),
|
|
|
- @(CStandardStampTypeConfidential),
|
|
|
- @(CStandardStampTypeForpublicrelease),
|
|
|
- @(CStandardStampTypeNotforpublicrelease),
|
|
|
- @(CStandardStampTypeForcomment),
|
|
|
- @(CStandardStampTypeVoid),
|
|
|
- @(CStandardStampTypePreliminaryresults),
|
|
|
- @(CStandardStampTypeInformationonly),
|
|
|
- @(CStandardStampTypeWitness),
|
|
|
- @(CStandardStampTypeInitialhere),
|
|
|
- @(CStandardStampTypeSignhere),
|
|
|
- @(CStandardStampTypeRevised),
|
|
|
- @(CStandardStampTypeAccepted),
|
|
|
- @(CStandardStampTypeRejected),
|
|
|
- @(CStandardStampTypeChick),
|
|
|
- @(CStandardStampTypeCross),
|
|
|
- @(CStandardStampTypeCircle),
|
|
|
- @(CStandardStampTypeEmergency),
|
|
|
- @(CStandardStampTypeExpired),
|
|
|
- @(CStandardStampTypeReceived),
|
|
|
- @(CStandardStampTypeReviewed),
|
|
|
- @(CStandardStampTypeVerified),
|
|
|
- @(CStandardStampTypeAccepteder),
|
|
|
- @(CStandardStampTypeRejecteder)];
|
|
|
- for(int i = 0;i<standarArray.count;i++) {
|
|
|
- NSNumber *index = standarArray[i];
|
|
|
- CStampObject *object = [[CStampObject alloc] initWithStandardStampType:[index intValue]];
|
|
|
-
|
|
|
- [dynamic addObject:object];
|
|
|
- }
|
|
|
- return dynamic;
|
|
|
-}
|
|
|
-
|
|
|
-- (NSMutableArray*)dynamicStamps {
|
|
|
- NSMutableArray *dynamicStampArray = [[NSMutableArray alloc] init];
|
|
|
- //从沙盒中取出日期格式
|
|
|
- NSString *dateStyleIndicate = [[NSUserDefaults standardUserDefaults] objectForKey:CPDFDynamicStampDateStyleKey1];
|
|
|
- NSString *dateFormatterString = @"";
|
|
|
- if (dateStyleIndicate.length < 1) {
|
|
|
- dateFormatterString = self.dateStyleArray.lastObject;
|
|
|
- } else {
|
|
|
- NSInteger index = [self.dateStyleArray indexOfObject:dateStyleIndicate];
|
|
|
- if (index > self.dateStyleArray.count) {
|
|
|
- dateFormatterString = self.dateStyleArray.lastObject;
|
|
|
- }else{
|
|
|
- dateFormatterString = dateStyleIndicate;
|
|
|
- }
|
|
|
- }
|
|
|
- NSString *dateString = [self setDynamicStampDateStringWithDateStyle:dateFormatterString];
|
|
|
- NSArray *dynamicArray = @[@"REVISED",@"REVIEWED",@"RECEIVED",@"APPROVED",@"Michael"];
|
|
|
- NSString *author = [[NSUserDefaults standardUserDefaults] stringForKey:@"CKUserName"];
|
|
|
- //从沙盒中取出是否需要作者
|
|
|
- BOOL isNeedAuthor = [[NSUserDefaults standardUserDefaults] boolForKey:CPDFDynamicStampNeedAuthorKey1];
|
|
|
- if(!isNeedAuthor) {
|
|
|
- author = [author length] ? author : NSFullUserName();
|
|
|
- dateString = [NSString stringWithFormat:@"By %@ at %@",author,dateString];
|
|
|
- } else {
|
|
|
- author = @"";
|
|
|
- }
|
|
|
-
|
|
|
- [dynamicArray enumerateObjectsUsingBlock:^(NSString* _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
|
|
- CPDFStampStyle style = CPDFStampStyleRed;
|
|
|
- if(idx>0 && idx<4) {
|
|
|
- style = CPDFStampStyleGreen;
|
|
|
- }
|
|
|
- CStampObject *stamp = [[CStampObject alloc] initWithDynamicText:obj dynamicDetailText:dateString dynamicStyle:style needExchangeFontSize:NO dynamicShape:CPDFStampShapeRectangle];
|
|
|
- if(idx == 4) {
|
|
|
- stamp = [[CStampObject alloc] initWithDynamicText:obj dynamicDetailText:dateString dynamicStyle:style needExchangeFontSize:YES dynamicShape:CPDFStampShapeRectangle];
|
|
|
- }
|
|
|
- [dynamicStampArray addObject:stamp];
|
|
|
- }];
|
|
|
-
|
|
|
- return dynamicStampArray;
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-#pragma mark -- Public
|
|
|
-
|
|
|
-- (BOOL)addStampWithString:(NSString *)string categoryString:(CPDFStampType )categoryString nameString:(NSString *)nameString dateString:(NSString *)dateString needExchangeFontSize:(BOOL)needExchange color:(CPDFStampStyle)color shape:(CPDFStampShape)shape date:(NSString *)date time:(NSString *)time
|
|
|
-{
|
|
|
- if (![[NSFileManager defaultManager] fileExistsAtPath:kStampFolderPath]) {
|
|
|
- if (![[NSFileManager defaultManager] createDirectoryAtPath:kStampFolderPath withIntermediateDirectories:YES attributes:nil error:nil]) {
|
|
|
- return NO;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (![[NSFileManager defaultManager] fileExistsAtPath:kStampPlistPath]) {
|
|
|
- if (![[NSFileManager defaultManager] createFileAtPath:kStampPlistPath contents:nil attributes:nil]) {
|
|
|
- return NO;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:kStampPlistPath];
|
|
|
- NSMutableDictionary *newDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionary];
|
|
|
-
|
|
|
- NSString *tag = [self tagString];
|
|
|
- NSString *text = @"";
|
|
|
- NSString *detailText = @"";
|
|
|
-
|
|
|
- NSMutableDictionary *stampDictionary = [[NSMutableDictionary alloc] init];
|
|
|
- if (string) {
|
|
|
- [stampDictionary setObject:string forKey:kStampOfStringKey];
|
|
|
- text = string;
|
|
|
- }
|
|
|
- if (dateString) {
|
|
|
- [stampDictionary setObject:dateString forKey:kStampOfSubStringKey];
|
|
|
- detailText = dateString;
|
|
|
- }
|
|
|
- if (date) {
|
|
|
- [stampDictionary setObject:date forKey:kStampDateKey];
|
|
|
- }
|
|
|
- if (time) {
|
|
|
- [stampDictionary setObject:time forKey:kStampTimeKey];
|
|
|
- }
|
|
|
- [stampDictionary setObject:@(categoryString) forKey:kStampOfCategoryKey];
|
|
|
-
|
|
|
- if(nameString) {
|
|
|
- [stampDictionary setObject:nameString forKey:kStampOfNameKey];
|
|
|
- detailText = [NSString stringWithFormat:@"%@ %@",detailText,nameString];
|
|
|
- }
|
|
|
- [stampDictionary setObject:[NSNumber numberWithInteger:color] forKey:kStampOfColorKey];
|
|
|
- [stampDictionary setObject:[NSDate date] forKey:kStampOfDateKey];
|
|
|
- [stampDictionary setObject:needExchange? @"1":@"0" forKey:kStampNeedExchangKey];
|
|
|
- [newDictionary setObject:stampDictionary forKey:tag];
|
|
|
-
|
|
|
- CStampObject *stamp = nil;
|
|
|
- if (categoryString == 4) {
|
|
|
-
|
|
|
- } else if (categoryString == 1) {
|
|
|
- stamp = [[CStampObject alloc] initWithText:text detailText:dateString style:color shape:shape];
|
|
|
- stamp.dateString = date;
|
|
|
- stamp.timeString = time;
|
|
|
- }
|
|
|
- if ([newDictionary writeToFile:kStampPlistPath atomically:YES] && stamp) {
|
|
|
- stamp.stampKey = tag;
|
|
|
- [self.stamps addObject:stamp];
|
|
|
- return YES;
|
|
|
- }
|
|
|
- return NO;
|
|
|
-}
|
|
|
-
|
|
|
-- (BOOL)addStampWithImagePath:(NSString *)path categoryString:(CPDFStampType)categoryString nameString:(NSString *)nameString
|
|
|
-{
|
|
|
- if (![[NSFileManager defaultManager] fileExistsAtPath:kStampFolderPath]) {
|
|
|
- if (![[NSFileManager defaultManager] createDirectoryAtPath:kStampFolderPath withIntermediateDirectories:YES attributes:nil error:nil]) {
|
|
|
- return NO;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (![[NSFileManager defaultManager] fileExistsAtPath:kStampPlistPath]) {
|
|
|
- if (![[NSFileManager defaultManager] createFileAtPath:kStampPlistPath contents:nil attributes:nil]) {
|
|
|
- return NO;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- NSString *tag = [self tagString];
|
|
|
- NSString *rPath = [kStampFolderPath stringByAppendingPathComponent:[tag stringByAppendingPathExtension:@"jpg"]];
|
|
|
-
|
|
|
- NSImage *image = [[NSImage alloc] initWithContentsOfFile:path];
|
|
|
- [image lockFocus];
|
|
|
- NSBitmapImageRep *bits = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0, 0, image.size.width, image.size.height)];
|
|
|
- [image unlockFocus];
|
|
|
- NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.5] forKey:NSImageCompressionFactor];
|
|
|
- NSData *imageData = [bits representationUsingType:NSJPEGFileType properties:imageProps];
|
|
|
- if (![imageData writeToFile:rPath atomically:YES]) {
|
|
|
- return NO;
|
|
|
- }
|
|
|
-
|
|
|
- NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:kStampPlistPath];
|
|
|
- NSMutableDictionary *newDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionary];
|
|
|
-
|
|
|
- CStampObject *stamp = [[CStampObject alloc] initWithImageStampFilePath:rPath];
|
|
|
- stamp.nameString = nameString;
|
|
|
-
|
|
|
- NSMutableDictionary *stampDictionary = [[NSMutableDictionary alloc] init];
|
|
|
- [stampDictionary setObject:[rPath lastPathComponent] forKey:kStampOfImagePathKey];
|
|
|
- [stampDictionary setObject:[NSDate date] forKey:kStampOfDateKey];
|
|
|
-
|
|
|
- [stampDictionary setObject:@(categoryString) forKey:kStampOfCategoryKey];
|
|
|
-
|
|
|
- if(nameString) {
|
|
|
- [stampDictionary setObject:nameString forKey:kStampOfNameKey];
|
|
|
- }
|
|
|
- [newDictionary setObject:stampDictionary forKey:tag];
|
|
|
-
|
|
|
- if ([newDictionary writeToFile:kStampPlistPath atomically:YES]) {
|
|
|
- stamp.stampKey = tag;
|
|
|
- [self.stamps addObject:stamp];
|
|
|
- return YES;
|
|
|
- }
|
|
|
-
|
|
|
- return NO;
|
|
|
-}
|
|
|
-
|
|
|
-- (BOOL)addStampWithImagePath:(NSString *)path categoryString:(CPDFStampType)categoryString nameString:(NSString *)nameString isRemoveBGColor:(BOOL)isRemoveBGColor
|
|
|
-{
|
|
|
- if (![[NSFileManager defaultManager] fileExistsAtPath:kStampFolderPath]) {
|
|
|
- if (![[NSFileManager defaultManager] createDirectoryAtPath:kStampFolderPath withIntermediateDirectories:YES attributes:nil error:nil]) {
|
|
|
- return NO;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- if (![[NSFileManager defaultManager] fileExistsAtPath:kStampPlistPath]) {
|
|
|
- if (![[NSFileManager defaultManager] createFileAtPath:kStampPlistPath contents:nil attributes:nil]) {
|
|
|
- return NO;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- NSString *tag = [self tagString];
|
|
|
- NSString *rPath = [kStampFolderPath stringByAppendingPathComponent:[tag stringByAppendingPathExtension:@"jpg"]];
|
|
|
-
|
|
|
- if (isRemoveBGColor) {
|
|
|
- NSImage *image = [[NSImage alloc] initWithContentsOfFile:path];
|
|
|
- NSData *imageData = [image TIFFRepresentation];
|
|
|
- CGImageRef imageRef;
|
|
|
- if (imageData) {
|
|
|
- CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
|
|
|
- imageRef = CGImageSourceCreateImageAtIndex(imageSource, 0, NULL);
|
|
|
- }
|
|
|
-
|
|
|
- const int imageWidth = image.size.width;
|
|
|
- const int imageHeight = image.size.height;
|
|
|
- size_t bytesPerRow = imageWidth * 4;
|
|
|
- uint32_t* rgbImageBuf = (uint32_t*)malloc(bytesPerRow * imageHeight);
|
|
|
-
|
|
|
- CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
|
|
- CGContextRef context = CGBitmapContextCreate(rgbImageBuf,
|
|
|
- imageWidth,
|
|
|
- imageHeight,
|
|
|
- 8,
|
|
|
- bytesPerRow,
|
|
|
- colorSpace,
|
|
|
- kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast);
|
|
|
- CGContextDrawImage(context, CGRectMake(0, 0, imageWidth, imageHeight), imageRef);
|
|
|
-
|
|
|
- int pixelNum = imageWidth * imageHeight;
|
|
|
- uint32_t* pCurPtr = rgbImageBuf;
|
|
|
- for (int i = 0; i < pixelNum; i++, pCurPtr++) {
|
|
|
- uint8_t* ptr = (uint8_t*)pCurPtr;
|
|
|
- if (ptr[1] > 240 && ptr[2] > 240 && ptr[3] > 240) {
|
|
|
- ptr[0] = 0;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- CGDataProviderRef dataProvider = CGDataProviderCreateWithData(NULL, rgbImageBuf, bytesPerRow * imageHeight, nil);
|
|
|
- imageRef = CGImageCreate(imageWidth,
|
|
|
- imageHeight,
|
|
|
- 8,
|
|
|
- 32,
|
|
|
- bytesPerRow,
|
|
|
- colorSpace,
|
|
|
- kCGImageAlphaLast |kCGBitmapByteOrder32Little,
|
|
|
- dataProvider,
|
|
|
- NULL,
|
|
|
- true,
|
|
|
- kCGRenderingIntentDefault);
|
|
|
- CGDataProviderRelease(dataProvider);
|
|
|
-
|
|
|
- NSImage *newImage = nil;
|
|
|
- if (imageRef) {
|
|
|
- NSRect imageRect = NSMakeRect(0.0, 0.0, 0.0, 0.0);
|
|
|
- CGContextRef imageContext = nil;
|
|
|
-
|
|
|
- // Get the image dimensions.
|
|
|
- imageRect.size.height = CGImageGetHeight(imageRef);
|
|
|
- imageRect.size.width = CGImageGetWidth(imageRef);
|
|
|
-
|
|
|
- // Create a new image to receive the Quartz image data.
|
|
|
- newImage = [[NSImage alloc] initWithSize:imageRect.size];
|
|
|
- [newImage lockFocus];
|
|
|
-
|
|
|
- // Get the Quartz context and draw.
|
|
|
- imageContext = (CGContextRef)[[NSGraphicsContext currentContext] graphicsPort];
|
|
|
- CGContextDrawImage(imageContext, *(CGRect*)&imageRect, imageRef);
|
|
|
- [newImage unlockFocus];
|
|
|
- CGImageRelease(imageRef);
|
|
|
- }
|
|
|
- image = newImage;
|
|
|
-
|
|
|
- [image lockFocus];
|
|
|
- NSBitmapImageRep *bits = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0, 0, image.size.width, image.size.height)];
|
|
|
- [image unlockFocus];
|
|
|
- NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor];
|
|
|
- imageData = [bits representationUsingType:NSPNGFileType properties:imageProps];
|
|
|
- if (![imageData writeToFile:rPath atomically:YES]) {
|
|
|
- return NO;
|
|
|
- }
|
|
|
- } else {
|
|
|
- if ([path.pathExtension.lowercaseString isEqualToString:@"pdf"]) {
|
|
|
- NSImage *image = [[NSImage alloc] initWithContentsOfFile:path];
|
|
|
- NSData *imageData = [image TIFFRepresentation];
|
|
|
- if (![imageData writeToFile:rPath atomically:YES]) {
|
|
|
- return NO;
|
|
|
- }
|
|
|
- } else {
|
|
|
- if (![[NSFileManager defaultManager] copyItemAtPath:path toPath:rPath error:nil]) {
|
|
|
- return NO;
|
|
|
- }
|
|
|
-
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:kStampPlistPath];
|
|
|
- NSMutableDictionary *newDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionary];
|
|
|
-
|
|
|
-// KMPDFStamp *stamp = [[[KMPDFStamp alloc] init] autorelease];
|
|
|
-// stamp.isImageStamp = YES;
|
|
|
-// stamp.date = [NSDate date];
|
|
|
-// stamp.imagePath = rPath;
|
|
|
-// stamp.tag = tag;
|
|
|
-// stamp.categoryString = categoryString;
|
|
|
-// stamp.nameString = nameString;
|
|
|
-
|
|
|
- NSMutableDictionary *stampDictionary = [[NSMutableDictionary alloc] init];
|
|
|
- [stampDictionary setObject:[rPath lastPathComponent] forKey:kStampOfImagePathKey];
|
|
|
- [stampDictionary setObject:[NSDate date] forKey:kStampOfDateKey];
|
|
|
- [stampDictionary setObject:@(categoryString) forKey:kStampOfCategoryKey];
|
|
|
- if(nameString) {
|
|
|
- [stampDictionary setObject:nameString forKey:kStampOfNameKey];
|
|
|
- }
|
|
|
-
|
|
|
- [newDictionary setObject:stampDictionary forKey:tag];
|
|
|
-
|
|
|
- if ([newDictionary writeToFile:kStampPlistPath atomically:YES]) {
|
|
|
-// [self.stamps addObject:stamp];
|
|
|
-// if(![self.stampCategorys containsObject:categoryString]) {
|
|
|
-// [self.stampCategorys addObject:categoryString];
|
|
|
-// }
|
|
|
- }
|
|
|
- return NO;
|
|
|
-}
|
|
|
-
|
|
|
-- (BOOL)removeStampAtIndex:(NSInteger)index
|
|
|
-{
|
|
|
- if (index < self.stamps.count) {
|
|
|
- CStampObject *stamp = [self.stamps objectAtIndex:index];
|
|
|
- NSString *key = stamp.stampKey;
|
|
|
-
|
|
|
- if (![[NSFileManager defaultManager] fileExistsAtPath:kStampPlistPath]) {
|
|
|
- return NO;
|
|
|
- }
|
|
|
-
|
|
|
- NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:kStampPlistPath];
|
|
|
- NSMutableDictionary *newDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionary];
|
|
|
-
|
|
|
- [newDictionary removeObjectForKey:@"key"];
|
|
|
-
|
|
|
- if ([newDictionary writeToFile:kStampPlistPath atomically:YES]) {
|
|
|
- if (stamp.imagePath && stamp.imagePath.length > 0) {
|
|
|
- [[NSFileManager defaultManager] removeItemAtPath:stamp.imagePath error:nil];
|
|
|
- }
|
|
|
- [self.stamps removeObject:stamp];
|
|
|
- return YES;
|
|
|
- }
|
|
|
- }
|
|
|
- return NO;
|
|
|
-}
|
|
|
-
|
|
|
-- (BOOL)removeStamp:(CStampObject *)stamp {
|
|
|
- if(!stamp) {
|
|
|
- return NO;
|
|
|
- }
|
|
|
- if([self.stamps containsObject:stamp]) {
|
|
|
- NSString *key = stamp.stampKey;
|
|
|
-
|
|
|
- if (![[NSFileManager defaultManager] fileExistsAtPath:kStampPlistPath]) {
|
|
|
- return NO;
|
|
|
- }
|
|
|
-
|
|
|
- NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:kStampPlistPath];
|
|
|
- NSMutableDictionary *newDictionary = [NSMutableDictionary dictionaryWithDictionary:dictionary];
|
|
|
-
|
|
|
- [newDictionary removeObjectForKey:key];
|
|
|
-
|
|
|
- if ([newDictionary writeToFile:kStampPlistPath atomically:YES]) {
|
|
|
- if (stamp.imagePath && stamp.imagePath.length > 0) {
|
|
|
- [[NSFileManager defaultManager] removeItemAtPath:stamp.imagePath error:nil];
|
|
|
- }
|
|
|
- [self.stamps removeObject:stamp];
|
|
|
- return YES;
|
|
|
- }
|
|
|
- }
|
|
|
- return NO;
|
|
|
-}
|
|
|
-
|
|
|
-- (void)addStamp:(CStampObject *)stamp {
|
|
|
- if(!stamp)
|
|
|
- return;
|
|
|
- if (stamp.stampCategoryType == CPDFStampTypeText) {
|
|
|
- [self addStampWithString:stamp.text categoryString:CPDFStampTypeText nameString:@"" dateString:stamp.detailText needExchangeFontSize:NO color:stamp.style shape:stamp.shape date:stamp.dateString time:stamp.timeString];
|
|
|
- } else if(stamp.stampCategoryType == CPDFStampTypeImage) {
|
|
|
- [self addStampWithImagePath:stamp.imagePath categoryString:CPDFStampTypeImage nameString:@""];
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-//根据不同的日期格式字符串获取最终的dateString
|
|
|
-- (NSString *)setDynamicStampDateStringWithDateStyle:(NSString *)dateStyleString
|
|
|
-{
|
|
|
- NSDate *now = [NSDate date];
|
|
|
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
|
|
|
-
|
|
|
- if ([dateStyleString isEqualToString:@"m/d"]) {
|
|
|
- formatter.dateFormat = @"M/dd HH:mm:ss";
|
|
|
- }else if ([dateStyleString isEqualToString:@"m/d/yy"]){
|
|
|
- formatter.dateFormat = @"M/dd/yy HH:mm:ss";
|
|
|
- }else if ([dateStyleString isEqualToString:@"m/d/yyyy"]){
|
|
|
- formatter.dateFormat = @"MM/dd/yyyy HH:mm:ss";
|
|
|
- }else if ([dateStyleString isEqualToString:@"mm/dd/yy"]){
|
|
|
- formatter.dateFormat = @"MM/dd/yy HH:mm:ss";
|
|
|
- }else if ([dateStyleString isEqualToString:@"mm/dd/yyyy"]){
|
|
|
- formatter.dateFormat = @"MM/dd/yyyy HH:mm:ss";
|
|
|
- }else if ([dateStyleString isEqualToString:@"d/m/yy"]){
|
|
|
- formatter.dateFormat = @"d/m/yy HH:mm:ss";
|
|
|
- }else if ([dateStyleString isEqualToString:@"d/m/yyyy"]){
|
|
|
- formatter.dateFormat = @"d/m/yyyy HH:mm:ss";
|
|
|
- }else if ([dateStyleString isEqualToString:@"dd/mm/yy"]){
|
|
|
- formatter.dateFormat = @"dd/MM/yy HH:mm:ss";
|
|
|
- }else if ([dateStyleString isEqualToString:@"dd/mm/yyyy"]){
|
|
|
- formatter.dateFormat = @"dd/mm/yyyy HH:mm:ss";
|
|
|
- }else if ([dateStyleString isEqualToString:@"mm/yy"]){
|
|
|
- formatter.dateFormat = @"MM/yy HH:mm:ss";
|
|
|
- }else if ([dateStyleString isEqualToString:@"mm/yyyy"]){
|
|
|
- formatter.dateFormat = @"MM/yyyy HH:mm:ss";
|
|
|
- }else if ([dateStyleString isEqualToString:@"m.d.yy"]){
|
|
|
- formatter.dateFormat = @"M.d.yy HH:mm:ss";
|
|
|
- }else if ([dateStyleString isEqualToString:@"m.d.yyyy"]){
|
|
|
- formatter.dateFormat = @"M.d.yyyy HH:mm:ss";
|
|
|
- }else if ([dateStyleString isEqualToString:@"mm.dd.yy"]){
|
|
|
- formatter.dateFormat = @"MM.dd.yy HH:mm:ss";
|
|
|
- }
|
|
|
- else if ([dateStyleString isEqualToString:@"mm.dd.yyyy"]){
|
|
|
- formatter.dateFormat = @"MM.dd.yyyy HH:mm:ss";
|
|
|
- }
|
|
|
- else if ([dateStyleString isEqualToString:@"mm.yy"]){
|
|
|
- formatter.dateFormat = @"MM.yyyy HH:mm:ss";
|
|
|
- }
|
|
|
- else if ([dateStyleString isEqualToString:@"mm.yyyy"]){
|
|
|
- formatter.dateFormat = @"MM.yyyy HH:mm:ss";
|
|
|
- }
|
|
|
- else if ([dateStyleString isEqualToString:@"d.m.yy"]){
|
|
|
- formatter.dateFormat = @"d.M.yy HH:mm:ss";
|
|
|
- }
|
|
|
- else if ([dateStyleString isEqualToString:@"d.m.yyyy"]){
|
|
|
- formatter.dateFormat = @"d.M.yyyy HH:mm:ss";
|
|
|
- }
|
|
|
- else if ([dateStyleString isEqualToString:@"dd.mm.yy"]){
|
|
|
- formatter.dateFormat = @"dd.MM.yy HH:mm:ss";
|
|
|
- }
|
|
|
- else if ([dateStyleString isEqualToString:@"dd.mm.yyyy"]){
|
|
|
- formatter.dateFormat = @"d.MM.yyyy HH:mm:ss";
|
|
|
- }
|
|
|
- else if ([dateStyleString isEqualToString:@"yy-mm-dd"]){
|
|
|
- formatter.dateFormat = @"yy-MM-dd HH:mm:ss";
|
|
|
- }
|
|
|
- else if ([dateStyleString isEqualToString:@"yyyy-mm-dd"]){
|
|
|
- formatter.dateFormat = @"yyy-MM-dd HH:mm:ss";
|
|
|
- } else {
|
|
|
- formatter.dateStyle = kCFDateFormatterMediumStyle;
|
|
|
- formatter.timeStyle = kCFDateFormatterShortStyle;
|
|
|
- }
|
|
|
-
|
|
|
- NSString *str = [formatter stringFromDate:now];
|
|
|
-
|
|
|
- return str;
|
|
|
-}
|
|
|
-
|
|
|
-- (NSArray *)dateStyleArray {
|
|
|
- return @[@"m/d",
|
|
|
- @"m/d/yy",
|
|
|
- @"m/d/yyyy",
|
|
|
- @"mm/dd/yy",
|
|
|
- @"mm/dd/yyyy",
|
|
|
- @"d/m/yy",
|
|
|
- @"d/m/yyyy",
|
|
|
- @"dd/mm/yy",
|
|
|
- @"dd/mm/yyyy",
|
|
|
- @"mm/yy",
|
|
|
- @"mm/yyyy",
|
|
|
- @"m.d.yy",
|
|
|
- @"m.d.yyyy",
|
|
|
- @"mm.dd.yy",
|
|
|
- @"mm.dd.yyyy",
|
|
|
- @"mm.yy",
|
|
|
- @"mm.yyyy",
|
|
|
- @"d.m.yy",
|
|
|
- @"d.m.yyyy",
|
|
|
- @"dd.mm.yy",
|
|
|
- @"dd.mm.yyyy",
|
|
|
- @"yy-mm-dd",
|
|
|
- @"yyyy-mm-dd",
|
|
|
- NSLocalizedString(@"Default", nil)];
|
|
|
-}
|
|
|
-
|
|
|
-@end
|