【转】图片转PDF的实现方法
[转载自]http://blog.csdn.net/yiyaaixuexi/article/details/8209188
Quartz 2D概述
Quartz 2D是Core Grapgics下的2D绘图引擎,适用于iOS和Mac OS X。 它具有强大的绘图功能,提供视角效果渲染,反锯齿渲染,颜色管理,绘制PDF等等。
开发者无需考虑不同设备不同分别率的显示效果,因为这一切适配操作Quartz 全都自己包了。
具体Quartz可以做些什么,下图给出了最佳答案:
图片转PDF的工具类
利用Quartz 2D将图片数据转换成图片不失真的PDF,方法我已经封装好:
(PDF部分属性未列出,如果有额外需要,需要另行提出赋值)
- //
- // WQPDFManager.h
- // wqphototopdf
- //
- // Created by Wu Qian on 12-10-22.
- //
- //
- #import <Foundation/Foundation.h>
- @interface WQPDFManager : NSObject
- /**
- * @brief 创建PDF文件
- *
- * @param imgData NSData型 照片数据
- * @param destFileName NSString型 生成的PDF文件名
- * @param pw NSString型 要设定的密码
- */
- + (void)WQCreatePDFFileWithSrc:(NSData *)imgData
- toDestFile:(NSString *)destFileName
- withPassword:(NSString *)pw;
- /**
- * @brief 抛出pdf文件存放地址
- *
- * @param filename NSString型 文件名
- *
- * @return NSString型 地址
- */
- + (NSString *)pdfDestPath:(NSString *)filename;
- @end
- //
- // WQPDFManager.m
- // wqphototopdf
- //
- // Created by Wu Qian on 12-10-22.
- //
- //
- #import "WQPDFManager.h"
- #import "WQPathUtilities.h"
- @implementation WQPDFManager
- void WQDrawContent(CGContextRef myContext,
- CFDataRef data,
- CGRect rect)
- {
- CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData(data);
- CGImageRef image = CGImageCreateWithJPEGDataProvider(dataProvider,
- NULL,
- NO,
- kCGRenderingIntentDefault);
- CGContextDrawImage(myContext, rect, image);
- CGDataProviderRelease(dataProvider);
- CGImageRelease(image);
- }
- void MyCreatePDFFile (CFDataRef data,
- CGRect pageRect,
- const char *filepath,
- CFStringRef password)
- {
- CGContextRef pdfContext;
- CFStringRef path;
- CFURLRef url;
- CFDataRef boxData = NULL;
- CFMutableDictionaryRef myDictionary = NULL;
- CFMutableDictionaryRef pageDictionary = NULL;
- path = CFStringCreateWithCString (NULL, filepath,
- kCFStringEncodingUTF8);
- url = CFURLCreateWithFileSystemPath (NULL, path,
- kCFURLPOSIXPathStyle, 0);
- CFRelease (path);
- myDictionary = CFDictionaryCreateMutable(NULL,
- 0,
- &kCFTypeDictionaryKeyCallBacks,
- &kCFTypeDictionaryValueCallBacks);
- CFDictionarySetValue(myDictionary,
- kCGPDFContextTitle,
- CFSTR("Photo from iPrivate Album"));
- CFDictionarySetValue(myDictionary,
- kCGPDFContextCreator,
- CFSTR("iPrivate Album"));
- if (password) {
- CFDictionarySetValue(myDictionary, kCGPDFContextUserPassword, password);
- CFDictionarySetValue(myDictionary, kCGPDFContextOwnerPassword, password);
- }
- pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary);
- CFRelease(myDictionary);
- CFRelease(url);
- pageDictionary = CFDictionaryCreateMutable(NULL,
- 0,
- &kCFTypeDictionaryKeyCallBacks,
- &kCFTypeDictionaryValueCallBacks);
- boxData = CFDataCreate(NULL,(const UInt8 *)&pageRect, sizeof (CGRect));
- CFDictionarySetValue(pageDictionary, kCGPDFContextMediaBox, boxData);
- CGPDFContextBeginPage (pdfContext, pageDictionary);
- WQDrawContent(pdfContext,data,pageRect);
- CGPDFContextEndPage (pdfContext);
- CGContextRelease (pdfContext);
- CFRelease(pageDictionary);
- CFRelease(boxData);
- }
- + (NSString *)pdfDestPath:(NSString *)filename
- {
- return [[WQPathUtilities tmpDirectory]stringByAppendingPathComponent:filename];
- }
- + (void)WQCreatePDFFileWithSrc:(NSData *)imgData
- toDestFile:(NSString *)destFileName
- withPassword:(NSString *)pw
- {
- NSString *fileFullPath = [self pdfDestPath:destFileName];
- const char *path = [fileFullPath UTF8String];
- CFDataRef data = (__bridge CFDataRef)imgData;
- UIImage *image = [UIImage imageWithData:imgData];
- CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
- CFStringRef password = (__bridge CFStringRef)pw;
- MyCreatePDFFile(data,rect, path, password);
- }
- @end
调用方法
- NSData *data = [NSData dataWithContentsOfFile:your_image_path];
- NSString *pdfname = @"photoToPDF.pdf";
- [WQPDFManager WQCreatePDFFileWithSrc:data toDestFile:pdfname withPassword:nil];
如果需要创建带密码保护的PDF,第三个参数传入密码即可。
效果图
以 【私人相册】 中的截图示意一下...
将照片转换为密码保护的PDF,并发送EMail ,接收者需键入密码才可查看照片:
作者:CrazyPebble
出处:http://crazypebble.cnblogs.com/
欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。