Parcourir la source

【综合】CPDFDocument新增插入图片的分类

tangchao il y a 1 an
Parent
commit
a9a0577596

+ 8 - 0
PDF Office/PDF Master/Class/PDFWindowController/PDFListView/CPDFKitExtensions/CPDFDocumentExtensions/CPDFDocument+KMExtension.h

@@ -11,6 +11,14 @@ NS_ASSUME_NONNULL_BEGIN
 
 @interface CPDFDocument (KMExtension)
 
+/*
+ * 插入图片
+ * @param pageSize 图片大小
+ * @param imagePath 图片路径
+ * @param index 插入的索引
+ * @return 插入结果
+ * @discussion [CPDFDocument insertPage:withImage:atIndex] 进一步封装。有兼容 png 格式
+ */
 - (BOOL)km_insertPage:(CGSize)pageSize withImage:(NSString *)imagePath atIndex:(NSUInteger)index;
 
 @end

+ 6 - 4
PDF Office/PDF Master/Class/PDFWindowController/PDFListView/CPDFKitExtensions/CPDFDocumentExtensions/CPDFDocument+KMExtension.m

@@ -19,7 +19,7 @@
         return YES;
     }
     // 插入失败
-    // 转换为data
+    // 转换为 jpg, 再插入到 document
     NSData *data = [[[NSImage alloc] initWithContentsOfFile:imagePath] TIFFRepresentation];
     if (!data) {
         return NO;
@@ -33,19 +33,21 @@
     NSData *jpgData = [bitmap representationUsingType:NSBitmapImageFileTypeJPEG properties:@{}];
     NSString *appID = [[NSBundle mainBundle] bundleIdentifier];
     NSString *folderPath =  [[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:appID] stringByAppendingPathComponent:@"KMTemp"];
-    if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath]) {
+    if (![[NSFileManager defaultManager] fileExistsAtPath:folderPath]) { // 路径不存在,则去创建
         [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
     }
     NSString *filePath = [folderPath stringByAppendingPathComponent:@"temp_saveDocumentForCPDFDocumentInsertImage.jpg"];
     
     BOOL result = NO;
-    if ([jpgData writeToFile:filePath atomically:YES]) {
+    if ([jpgData writeToFile:filePath atomically:YES]) { // 将图片数据存入到临时路径
+        // 转换后再插入
         result = [self insertPage:pageSize withImage:filePath atIndex:index];
     }
+    
+    // 清除临时数据
     if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
         [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
     }
-    
     return result;
 }