// // UIImage+PDFUtility.h // Hello World // // Created by Erica Sadun on 8/14/14. // Copyright (c) 2014 Erica Sadun. All rights reserved. // #import <Foundation/Foundation.h> @interface UIImage_PDFUtility : NSObject UIImage *ImageFromPDFFile(NSString *pdfPath, CGSize targetSize); UIImage *ImageFromPDFFileWithWidth(NSString *pdfPath, CGFloat targetWidth); UIImage *ImageFromPDFFileWithHeight(NSString *pdfPath, CGFloat targetHeight); @end
// // UIImage+PDFUtility.m // Hello World // // Created by Erica Sadun on 8/14/14. // Copyright (c) 2014 Erica Sadun. All rights reserved. // #import "UIImage+PDFUtility.h" static CGFloat AspectScaleFit(CGSize sourceSize, CGRect destRect) { CGSize destSize = destRect.size; CGFloat scaleW = destSize.width / sourceSize.width; CGFloat scaleH = destSize.height / sourceSize.height; return fmin(scaleW, scaleH); } static CGRect RectAroundCenter(CGPoint center, CGSize size) { CGFloat halfWidth = size.width / 2.0f; CGFloat halfHeight = size.height / 2.0f; return CGRectMake(center.x - halfWidth, center.y - halfHeight, size.width, size.height); } static CGRect RectByFittingRect(CGRect sourceRect, CGRect destinationRect) { CGFloat aspect = AspectScaleFit(sourceRect.size, destinationRect); CGSize targetSize = CGSizeMake(sourceRect.size.width * aspect, sourceRect.size.height * aspect); CGPoint center = CGPointMake(CGRectGetMidX(destinationRect), CGRectGetMidY(destinationRect)); return RectAroundCenter(center, targetSize); } void DrawPDFPageInRect(CGPDFPageRef pageRef, CGRect destinationRect) { CGContextRef context = UIGraphicsGetCurrentContext(); if (context == NULL) { NSLog(@"Error: No context to draw to"); return; } CGContextSaveGState(context); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); // Flip the context to Quartz space CGAffineTransform transform = CGAffineTransformIdentity; transform = CGAffineTransformScale(transform, 1.0f, -1.0f); transform = CGAffineTransformTranslate(transform, 0.0f, -image.size.height); CGContextConcatCTM(context, transform); // Flip the rect, which remains in UIKit space CGRect d = CGRectApplyAffineTransform(destinationRect, transform); // Calculate a rectangle to draw to CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox); CGFloat drawingAspect = AspectScaleFit(pageRect.size, d); CGRect drawingRect = RectByFittingRect(pageRect, d); // Adjust the context CGContextTranslateCTM(context, drawingRect.origin.x, drawingRect.origin.y); CGContextScaleCTM(context, drawingAspect, drawingAspect); // Draw the page CGContextDrawPDFPage(context, pageRef); CGContextRestoreGState(context); } UIImage *ImageFromPDFFile(NSString *pdfPath, CGSize targetSize) { CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:pdfPath]); if (pdfRef == NULL) { NSLog(@"Error loading PDF"); return nil; } UIGraphicsBeginImageContextWithOptions(targetSize, NO, 0); CGPDFPageRef pageRef = CGPDFDocumentGetPage(pdfRef, 1); DrawPDFPageInRect(pageRef, (CGRect){.size = targetSize}); UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); CGPDFDocumentRelease(pdfRef); return image; } CGFloat GetPDFFileAspect(NSString *pdfPath) { CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithURL((__bridge CFURLRef)[NSURL fileURLWithPath:pdfPath]); if (pdfRef == NULL) { NSLog(@"Error loading PDF"); return 0.0; } CGPDFPageRef pageRef = CGPDFDocumentGetPage(pdfRef, 1); CGRect pageRect = CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox); CGPDFDocumentRelease(pdfRef); return pageRect.size.width / pageRect.size.height; } UIImage *ImageFromPDFFileWithWidth(NSString *pdfPath, CGFloat targetWidth) { CGFloat aspect = GetPDFFileAspect(pdfPath); if (aspect == 0.0) return nil; return ImageFromPDFFile(pdfPath, CGSizeMake(targetWidth, targetWidth / aspect)); } UIImage *ImageFromPDFFileWithHeight(NSString *pdfPath, CGFloat targetHeight) { CGFloat aspect = GetPDFFileAspect(pdfPath); if (aspect == 0.0) return nil; return ImageFromPDFFile(pdfPath, CGSizeMake(targetHeight * aspect, targetHeight)); }
I’ve posted before about how the new PDF support in asset catalogs doesn’t really do what we expect (offer resizable vector assets). To make up for that, here’s a class that loads images from PDF files directly, building them to whatever UIImage size you need. Supply a path to the image and a target size, height, or width. If using a size, the functions will scale-aspect-fit to that target. With width or height, they’ll use the built-in aspect and build an appropriately sized partner dimension.
This class is part of my useful things repo, which is where I stash stuff that people may generally find useful. A bunch of classes and utilities are tied to my InformIT write-ups, but others meander there over time because I was inspired for whatever reason.
我之前写过关于如何在资产目录,新的PDF格式的支持并没有我们所期望的(提供可调整大小的矢量资产)。为了弥补这一点,这个类加载图像从PDF文件直接,建设他们任何你需要的大小uiimage。提供一种图像的路径和目标的大小,高度,宽度。如果使用的尺寸,功能会规模方面适合目标。宽度或高度,他们就会使用内置的方面,建立一个适当大小的伙伴尺寸。
这门课是我回购部分有用的东西,这是我藏东西,人通常可以找到有用的。一类和公用事业与我Informit写UPS,但其他弯曲有因我的灵感是什么原因。
浙公网安备 33010602011771号