图片转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];