public
Last active — forked from PlacePop/README

Adds support for loading UIImage's from PDF files. I think it's a great way to add resolution-independent graphics to your apps, and also remove the "export to bitmap" step from your workflow. Just save directly from Illustrator!

  • Download Gist
README
1 2 3 4 5 6 7 8 9 10 11
Adds support for loading UIImage's from PDF files. I think it's a great way to add resolution-independent graphics to your apps, and also remove the "export to bitmap" step from your workflow. Just save directly from Illustrator!
 
What it does:
 
- You can pass the name of a PDF file (with or without the '.pdf') to +[UIImage imageNamed:]
- You can use the name of a PDF file inside Interface Builder, in your UIImageView's
- Turn a multi-page PDF into an array of UIImage's... good for animation.
 
Feedback to gists @nielsbot.com. Thanks!
 
.n
UIImage+imageWithPDFNamed.h
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
//
// UIImage+PDFSupport.h
//
// Created by Niels Gabel on 1/11/11.
// Copyright 2011 PlacePop Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
 
#import <Foundation/Foundation.h>
 
// This option adds support for using PDFs in UIImageView's in NIB files.
// (You can specify PDF files for image views and they will load in your app,
// but interface builder won't display them)
// Use this option at your own risk; you could potentially fall afoul
// of Apple's SPI rules, but I think it's unlikely.
#define UIImage_PDFSupport_SupportNIBLoading 0
 
// n.b.: including this category in your project will patch +[ UIImage imageNamed: ] to also support PDF files automagically, like this:
//
// UIImage * image = [ UIImage imageNamed:@"test.pdf" ] ;
//
 
@interface UIImage ( PDFSupport )
 
// returns an array containing images of the pages in a PDF
+(NSArray*)imagesWithPDFNamed:(NSString*)name scale:(CGFloat)scale ;
 
// returns an array containing images of the pages in a PDF, scaled by 'transform'
+(NSArray*)imagesWithPDFNamed:(NSString*)name scale:(CGFloat)scale transform:(CGAffineTransform)t ;
 
// returns an image of the first page in a PDF
+(UIImage*)imageWithPDFNamed:(NSString*)name scale:(CGFloat)scale ;
 
// returns an image of the first page in a PDF, scaled by 'transform'
+(UIImage*)imageWithPDFNamed:(NSString*)name scale:(CGFloat)scale transform:(CGAffineTransform)t ;
 
@end
UIImage+imageWithPDFNamed.m
Objective-C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
//
// UIImage+PDFSupport.m
//
// Created by Niels Gabel on 1/11/11.
// Copyright 2011 PlacePop Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
 
#import "UIImage+UIImagePDFSupport.h"
#import <CoreGraphics/CoreGraphics.h>
#import <objc/runtime.h>
 
#undef DebugLog
 
#ifndef DebugAssert
#define DebugAssert(x) assert((x))
#endif
 
 
static UIImage * (*old_UIImage_imageNamed)(id target, SEL selector, NSString * name ) = NULL ;
 
#if UIImage_PDFSupport_SupportNIBLoading
static UIImage * (*oldUIImageNibPlaceholder_InitWithCoder)(id, SEL, NSKeyedUnarchiver*) = NULL ;
static UIImage * UIImageNibPlaceholder_InitWithCoder( id self, SEL _cmd, NSKeyedUnarchiver * unarchiver )
{
NSString * resourceName = [ unarchiver decodeObjectForKey:@"UIResourceName" ] ;
UIImage * result = [ UIImage imageNamed:resourceName ] ;
if ( result )
{
[ self autorelease ] ;
return [ result retain ] ;
}
return (*oldUIImageNibPlaceholder_InitWithCoder)( self, _cmd, unarchiver ) ;
}
#endif
 
static UIImage * UIImageWithPDF( id target, SEL selector, NSString * name )
{
UIImage * result = (*old_UIImage_imageNamed)( target, selector, name ) ;
if ( !result )
{
result = [ UIImage imageWithPDFNamed:name scale:[ UIScreen mainScreen ].scale ] ;
}
return result ;
}
static CGPDFDocumentRef CreatePDFDocumentWithName( CFStringRef nameCFString )
{
if ( !nameCFString )
{
return NULL ;
}
NSString * name = (NSString*)nameCFString ;
name = [ name stringByDeletingPathExtension ] ;
NSString * path = [ [ NSBundle mainBundle ] pathForResource:name ofType:@"pdf" ] ;
if ( !path )
{
return NULL ;
}
CFURLRef url = CFAutorelease( CFURLCreateWithFileSystemPath( kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false ) ) ;
DebugAssert( url ) ;
CGPDFDocumentRef doc = CGPDFDocumentCreateWithURL( url ) ;
 
return doc ;
}
 
@implementation UIImage ( PDFSupport )
 
+(void)load
{
NSAutoreleasePool * pool = [[ NSAutoreleasePool alloc ] init ] ;
Method m = class_getClassMethod( [ UIImage class ], @selector( imageNamed: )) ;
old_UIImage_imageNamed = (UIImage *(*)(id, SEL, NSString*))method_setImplementation( m, (IMP)UIImageWithPDF ) ;
DebugAssert( old_UIImage_imageNamed ) ;
#if UIImage_PDFSupport_SupportNIBLoading
oldUIImageNibPlaceholder_InitWithCoder = (UIImage * (*)(id, SEL, NSKeyedUnarchiver*))class_replaceMethod( NSClassFromString(@"UIImageNibPlaceholder"), @selector( initWithCoder: ), (IMP)UIImageNibPlaceholder_InitWithCoder, "@@:@" ) ;
#endif
[ pool release ] ;
}
 
+(UIImage*)imageWithPDFPage:(CGPDFPageRef)page scale:(CGFloat)scale transform:(CGAffineTransform)t
{
if ( !page )
{
return nil ;
}
CGRect box = CGPDFPageGetBoxRect( page, kCGPDFCropBox ) ;
DebugAssert( box.size.width >= FLT_MIN && box.size.height >= FLT_MIN )
t = CGAffineTransformConcat( CGAffineTransformMakeScale( scale, scale ), t ) ;
box = CGRectApplyAffineTransform( box, t ) ;
size_t pixelWidth = box.size.width ;
CGColorSpaceRef cs = CGColorSpaceCreateDeviceRGB() ;
DebugAssert( cs ) ;
CGContextRef c = CGBitmapContextCreate( NULL, box.size.width, box.size.height, 8, 4 * pixelWidth, cs, kCGBitmapByteOrderDefault | kCGImageAlphaPremultipliedLast ) ;
CGColorSpaceRelease( cs ) ;
DebugAssert( c ) ;
CGContextConcatCTM( c, t ) ;
CGContextDrawPDFPage( c, page ) ;
CGImageRef cgImage = CGBitmapContextCreateImage( c ) ;
DebugAssert( cgImage ) ;
CGContextRelease( c ) ;
UIImage * image = image = [ UIImage imageWithCGImage:cgImage scale:scale orientation:UIImageOrientationUp ] ;
CGImageRelease( cgImage ) ;
return image ;
}
 
+(NSArray*)imagesWithPDFNamed:(NSString*)name scale:(CGFloat)scale transform:(CGAffineTransform)t
{
CGPDFDocumentRef doc = CreatePDFDocumentWithName( (CFStringRef)name ) ;
NSMutableArray * array = [ NSMutableArray array ] ;
 
// PDF pages are numbered starting at page 1
for( size_t pageIndex=1, numberOfPages = CGPDFDocumentGetNumberOfPages( doc ); pageIndex <= numberOfPages; ++pageIndex )
{
CGPDFPageRef page = CGPDFDocumentGetPage( doc, pageIndex ) ;
UIImage * image = [ UIImage imageWithPDFPage:page scale:scale transform:t ] ;
[ array addObject:image ? image : (UIImage*)[ NSNull null ] ] ;
}
return array ;
}
 
+(NSArray*)imagesWithPDFNamed:(NSString*)name scale:(CGFloat)scale
{
return [ self imagesWithPDFNamed:name scale:scale transform:CGAffineTransformIdentity ] ;
}
 
 
+(UIImage*)imageWithPDFNamed:(NSString*)name scale:(CGFloat)scale transform:(CGAffineTransform)t
{
CGPDFDocumentRef doc = CreatePDFDocumentWithName( (CFStringRef)name ) ;
if ( !doc )
{
return NULL ;
}
// PDF pages are numbered starting at page 1
CGPDFPageRef page = CGPDFDocumentGetPage( doc, 1 ) ;
DebugAssert( page ) ;
UIImage * result = [ UIImage imageWithPDFPage:page scale:scale transform:t ] ;
CFRelease( doc ) ;
return result ;
}
 
+(UIImage*)imageWithPDFNamed:(NSString*)name scale:(CGFloat)scale
{
return [ self imageWithPDFNamed:name scale:scale transform:CGAffineTransformIdentity ] ;
}
 
@end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.