Skip to content

Commit

Permalink
Xcode 6/iOS 8 compatible, code updates and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Parry committed Sep 10, 2014
1 parent 4fb1bdd commit 901aa1f
Show file tree
Hide file tree
Showing 40 changed files with 564 additions and 552 deletions.
714 changes: 357 additions & 357 deletions Xcode/PDFImage.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

14 changes: 6 additions & 8 deletions Xcode/PDFImage/PDFImage-Info.plist → Xcode/PDFImage/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>English</string>
<string>en</string>
<key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFile</key>
<string></string>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>com.tparry.${PRODUCT_NAME:rfc1034identifier}</string>
<string>com.tparry.PDFImage</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>${PRODUCT_NAME}</string>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>1.2</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1</string>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
Expand Down
4 changes: 3 additions & 1 deletion Xcode/PDFImage/PDFBarButtonItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
// For more information, please refer to <http://unlicense.org/>
//

#import <UIKit/UIKit.h>

@class PDFImage;

@interface PDFBarButtonItem : UIBarButtonItem

// Scales the image down proportionally to fit into a target size of 28x28
// for best results, the PDFImage should be as close to the target size as possible
- (id) initWithImage:(PDFImage*) image style:(UIBarButtonItemStyle) style target:(id) target action:(SEL) action;
- (instancetype) initWithImage:(PDFImage*) image style:(UIBarButtonItemStyle) style target:(id) target action:(SEL) action;

@end
11 changes: 6 additions & 5 deletions Xcode/PDFImage/PDFBarButtonItem.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,26 @@ @interface PDFBarButtonItem ()
@property (nonatomic, readonly) PDFImage* originalImage;
@property (nonatomic, readonly) CGSize targetSize;

- (void) updateBarButtonImage;

@end

@implementation PDFBarButtonItem

+ (void) initialize
{
isiOS7OrGreater = ([UIDevice currentDevice].systemVersion.integerValue >= 7);
if(self == [PDFBarButtonItem class])
{
isiOS7OrGreater = ([UIDevice currentDevice].systemVersion.integerValue >= 7);
}
}

#pragma mark -

- (id) initWithImage:(PDFImage*) image style:(UIBarButtonItemStyle) style target:(id) target action:(SEL) action
- (instancetype) initWithImage:(PDFImage*) image style:(UIBarButtonItemStyle) style target:(id) target action:(SEL) action
{
return [self initWithImage:image style:style target:target action:action targetSize:CGSizeMake(28, 28)];
}

- (id) initWithImage:(PDFImage*) image style:(UIBarButtonItemStyle) style target:(id) target action:(SEL) action targetSize:(CGSize) targetSize
- (instancetype) initWithImage:(PDFImage*) image style:(UIBarButtonItemStyle) style target:(id) target action:(SEL) action targetSize:(CGSize) targetSize
{
self = [super initWithImage:nil style:style target:target action:action];

Expand Down
15 changes: 0 additions & 15 deletions Xcode/PDFImage/PDFImage-Prefix.pch

This file was deleted.

14 changes: 7 additions & 7 deletions Xcode/PDFImage/PDFImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,18 @@

@property (nonatomic, readonly) CGSize size; // original page size

+ (PDFImage*) imageNamed:(NSString*) name; // from the main bundle, the .pdf extension can be omitted,
+ (instancetype) imageNamed:(NSString*) name; // from the main bundle, the .pdf extension can be omitted,
// this and +imageNamed:inBundle: are the only methods that will NSCache PDFImages, as bundles are read-only

+ (PDFImage*) imageNamed:(NSString*) name inBundle:(NSBundle*) bundle;
+ (instancetype) imageNamed:(NSString*) name inBundle:(NSBundle*) bundle;

+ (PDFImage*) imageWithContentsOfFile:(NSString*) path;
+ (PDFImage*) imageWithData:(NSData*) data;
+ (instancetype) imageWithContentsOfFile:(NSString*) path;
+ (instancetype) imageWithData:(NSData*) data;

- (id) initWithContentsOfFile:(NSString*) path;
- (id) initWithData:(NSData*) data;
- (instancetype) initWithContentsOfFile:(NSString*) path;
- (instancetype) initWithData:(NSData*) data;

- (id) initWithDocument:(CGPDFDocumentRef) document;
- (instancetype) initWithDocument:(CGPDFDocumentRef) document;

- (UIImage*) imageWithOptions:(PDFImageOptions*) options; // will NSCache the image if the same options are used again
- (void) drawInRect:(CGRect) rect;
Expand Down
73 changes: 34 additions & 39 deletions Xcode/PDFImage/PDFImage.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,23 @@

@interface PDFImage ()
{
CGPDFDocumentRef document;
CGPDFPageRef page;

NSCache* imageCache;
dispatch_once_t imageCacheOnceToken;
NSCache* _imageCache;
dispatch_once_t _imageCacheOnceToken;
}

@property (nonatomic, readonly) CGPDFDocumentRef document;
@property (nonatomic, readonly) CGPDFPageRef page;

@end

@implementation PDFImage

@synthesize size;

+ (PDFImage*) imageNamed:(NSString*) name
+ (instancetype) imageNamed:(NSString*) name
{
return [self imageNamed:name inBundle:[NSBundle mainBundle]];
}

+ (PDFImage*) imageNamed:(NSString*) name inBundle:(NSBundle*) bundle
+ (instancetype) imageNamed:(NSString*) name inBundle:(NSBundle*) bundle
{
// Defaults
NSString* pathName = name;
Expand Down Expand Up @@ -83,7 +81,7 @@ + (PDFImage*) imageNamed:(NSString*) name inBundle:(NSBundle*) bundle
return [self imageResource:pathName ofType:pathType inBundle:bundle];
}

+ (PDFImage*) imageResource:(NSString*) name ofType:(NSString*) type inBundle:(NSBundle*) bundle
+ (instancetype) imageResource:(NSString*) name ofType:(NSString*) type inBundle:(NSBundle*) bundle
{
NSString* filepath = [bundle pathForResource:name ofType:type];
NSString* cacheKey = filepath;
Expand All @@ -97,9 +95,7 @@ + (PDFImage*) imageResource:(NSString*) name ofType:(NSString*) type inBundle:(N

if(result == nil)
{
// Done in two parts to keep the type strict...
PDFImage* alloced = [self alloc];
result = [alloced initWithContentsOfFile:filepath];
result = [(PDFImage*)[self alloc] initWithContentsOfFile:filepath];

if(result != nil)
[sharedPDFImageCache setObject:result forKey:cacheKey];
Expand All @@ -108,51 +104,49 @@ + (PDFImage*) imageResource:(NSString*) name ofType:(NSString*) type inBundle:(N
return result;
}

+ (PDFImage*) imageWithContentsOfFile:(NSString*) path
+ (instancetype) imageWithContentsOfFile:(NSString*) path
{
PDFImage* alloced = [self alloc];
return [alloced initWithContentsOfFile:path];
return [(PDFImage*)[self alloc] initWithContentsOfFile:path];
}

+ (PDFImage*) imageWithData:(NSData*) data
+ (instancetype) imageWithData:(NSData*) data
{
PDFImage* alloced = [self alloc];
return [alloced initWithData:data];
return [(PDFImage*)[self alloc] initWithData:data];
}

- (id) initWithContentsOfFile:(NSString*) path
- (instancetype) initWithContentsOfFile:(NSString*) path
{
NSData* data = [[NSData alloc] initWithContentsOfFile:path];
return [self initWithData:data];
}

- (id) initWithData:(NSData*) data
- (instancetype) initWithData:(NSData*) data
{
CGDataProviderRef provider = CGDataProviderCreateWithCFData((__bridge CFDataRef)data);
CGPDFDocumentRef _document = CGPDFDocumentCreateWithProvider(provider);
CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(provider);
CGDataProviderRelease(provider);

id result = [self initWithDocument:_document];
id result = [self initWithDocument:document];

if(_document != nil)
CGPDFDocumentRelease(_document);
if(document != nil)
CGPDFDocumentRelease(document);

return result;
}

- (id) initWithDocument:(CGPDFDocumentRef) _document
- (instancetype) initWithDocument:(CGPDFDocumentRef) document
{
if(_document == nil)
if(document == nil)
return nil;

self = [super init];

if(self != nil)
{
document = CGPDFDocumentRetain(_document);
page = CGPDFDocumentGetPage(document, 1);
_document = CGPDFDocumentRetain(document);
_page = CGPDFDocumentGetPage(_document, 1);

size = CGPDFPageGetBoxRect(page, kCGPDFMediaBox).size;
_size = CGPDFPageGetBoxRect(_page, kCGPDFMediaBox).size;
}

return self;
Expand All @@ -164,19 +158,19 @@ - (id) initWithDocument:(CGPDFDocumentRef) _document
- (UIImage*) imageWithOptions:(PDFImageOptions*) options
{
// Where to draw the image
const CGRect rect = [options contentBoundsForContentSize:size];
const CGRect rect = [options contentBoundsForContentSize:self.size];

const CGFloat scale = options.scale;
UIColor* tintColor = [options.tintColor copy];
const CGSize containerSize = options.size;

NSString* cacheKey = [NSString stringWithFormat:@"%@-%0.2f-%@-%@", NSStringFromCGRect(rect), scale, tintColor.description, NSStringFromCGSize(containerSize)];

dispatch_once(&imageCacheOnceToken, ^{
imageCache = [[NSCache alloc] init];
dispatch_once(&_imageCacheOnceToken, ^{
_imageCache = [[NSCache alloc] init];
});

UIImage* image = [imageCache objectForKey:cacheKey];
UIImage* image = [_imageCache objectForKey:cacheKey];

if(image == nil)
{
Expand All @@ -203,7 +197,7 @@ - (UIImage*) imageWithOptions:(PDFImageOptions*) options
UIGraphicsEndImageContext();

if(image != nil)
[imageCache setObject:image forKey:cacheKey];
[_imageCache setObject:image forKey:cacheKey];
}

return image;
Expand All @@ -212,6 +206,7 @@ - (UIImage*) imageWithOptions:(PDFImageOptions*) options
- (void) drawInRect:(CGRect) rect
{
const CGSize drawSize = rect.size;
const CGSize size = self.size;
const CGSize sizeRatio = CGSizeMake(size.width / drawSize.width, size.height / drawSize.height);

CGContextRef ctx = UIGraphicsGetCurrentContext();
Expand All @@ -222,7 +217,7 @@ - (void) drawInRect:(CGRect) rect
CGContextScaleCTM(ctx, 1 / sizeRatio.width, 1 / -sizeRatio.height);
CGContextTranslateCTM(ctx, rect.origin.x * sizeRatio.width, (-drawSize.height - rect.origin.y) * sizeRatio.height);

CGContextDrawPDFPage(ctx, page);
CGContextDrawPDFPage(ctx, self.page);

CGContextRestoreGState(ctx);
}
Expand All @@ -232,10 +227,10 @@ - (void) drawInRect:(CGRect) rect

- (void) dealloc
{
if(document != nil)
if(_document != nil)
{
CGPDFDocumentRelease(document);
document = nil;
CGPDFDocumentRelease(_document);
_document = nil;
}
}

Expand Down
4 changes: 3 additions & 1 deletion Xcode/PDFImage/PDFImageOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
// For more information, please refer to <http://unlicense.org/>
//

#import <UIKit/UIKit.h>

@interface PDFImageOptions : NSObject

@property (nonatomic, assign) CGFloat scale; // screen scale, defaults to 0, the current screen scale
Expand All @@ -33,7 +35,7 @@
@property (nonatomic, assign) UIViewContentMode contentMode; // defaults to UIViewContentModeScaleToFill

// Convience method for simply spitting out a sized version
+ (PDFImageOptions*) optionsWithSize:(CGSize) size;
+ (instancetype) optionsWithSize:(CGSize) size;

- (CGRect) contentBoundsForContentSize:(CGSize) contentSize;

Expand Down
16 changes: 6 additions & 10 deletions Xcode/PDFImage/PDFImageOptions.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@

@implementation PDFImageOptions

@synthesize scale;
@synthesize tintColor;
@synthesize size;
@synthesize contentMode;

+ (PDFImageOptions*) optionsWithSize:(CGSize) size
+ (instancetype) optionsWithSize:(CGSize) size
{
PDFImageOptions* options = [[self alloc] init];
[options setSize:size];
Expand All @@ -43,13 +38,13 @@ + (PDFImageOptions*) optionsWithSize:(CGSize) size

#pragma mark -

- (id) init
- (instancetype) init
{
self = [super init];

if(self != nil)
{
contentMode = UIViewContentModeScaleToFill;
_contentMode = UIViewContentModeScaleToFill;
}

return self;
Expand All @@ -60,7 +55,8 @@ - (id) init

- (CGRect) contentBoundsForContentSize:(CGSize) contentSize
{
const CGSize containerSize = size;
const CGSize containerSize = self.size;
const UIViewContentMode contentMode = self.contentMode;

CGRect rect = CGRectZero;

Expand Down Expand Up @@ -154,7 +150,7 @@ - (CGRect) contentBoundsForContentSize:(CGSize) contentSize

- (CGSize) wholeProportionalFitForContentSize:(CGSize) contentSize
{
const CGSize containerSize = size;
const CGSize containerSize = self.size;

if(contentSize.width > containerSize.width || contentSize.height > containerSize.height)
{
Expand Down
2 changes: 2 additions & 0 deletions Xcode/PDFImage/PDFImageView.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
// For more information, please refer to <http://unlicense.org/>
//

#import <UIKit/UIKit.h>

@class PDFImage;

@interface PDFImageView : UIView
Expand Down
Loading

0 comments on commit 901aa1f

Please sign in to comment.