Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Prevent invalid path from crashing the entire app and get correct mediarect #9

Merged
merged 2 commits into from

2 participants

@mac-cain13

Hi!

This pull req contains 2 commits. One contains a fix for invalid paths, the library will now just return nil objects instead of crashing. This is more like UIImage is behaving in general and somewhat easier to work with if you generate filenames dynamically and it's possible that the image doesn't exists.

The second makes sure the correct mediarect is returned, if you rendered a image on page 3 (or any page other then 1) the mediarect from page 1 was used. That is not necesseraly the correct rect, this commit fixes that.

Hope you will take this changes in! Let me know if we need to change or tweak anything to make it in to your repos!

Cheers,
Mathijs Kadijk

mac-cain13 added some commits
@mac-cain13 mac-cain13 Fixed PDFView mediaRectForURL: calls
All calls now request the rect for the correct page.
0bb2426
@mac-cain13 mac-cain13 Invalid resource path/url will return nil UIImages
before this commit the App just crashed on invalid paths.
078d0be
@mindbrix mindbrix merged commit e6ea355 into from
@mindbrix
Owner

Hi Mathijs. Many thanks for these updates. It's good to have some help.

Best,
Nigel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 25, 2012
  1. @mac-cain13

    Fixed PDFView mediaRectForURL: calls

    mac-cain13 authored
    All calls now request the rect for the correct page.
  2. @mac-cain13

    Invalid resource path/url will return nil UIImages

    mac-cain13 authored
    before this commit the App just crashed on invalid paths.
This page is out of date. Refresh to see the latest.
Showing with 33 additions and 5 deletions.
  1. +9 −1 UIImage+PDF/PDFView.m
  2. +24 −4 UIImage+PDF/UIImage+PDF.m
View
10 UIImage+PDF/PDFView.m 100644 → 100755
@@ -93,7 +93,15 @@ +(int) pageCountForURL:(NSURL *)resourceURL
+(NSURL *)resourceURLForName:(NSString *)resourceName
{
- return ( resourceName ) ? [ NSURL fileURLWithPath:[[ NSBundle mainBundle ] pathForResource:resourceName ofType:nil ]] : nil;
+ NSString *path = [[ NSBundle mainBundle ] pathForResource:resourceName ofType:nil ];
+ if( path == nil )
+ {
+ return nil;
+ }
+ else
+ {
+ return ( resourceName ) ? [ NSURL fileURLWithPath:path] : nil;
+ }
}
View
28 UIImage+PDF/UIImage+PDF.m 100644 → 100755
@@ -188,8 +188,13 @@ +(UIImage *) imageWithPDFURL:(NSURL *)URL atSize:(CGSize)size
+(UIImage *) imageWithPDFURL:(NSURL *)URL fitSize:(CGSize)size atPage:(int)page
{
+ if ( URL == nil )
+ {
+ return nil;
+ }
+
// Get dimensions
- CGRect mediaRect = [ PDFView mediaRectForURL:URL ];
+ CGRect mediaRect = [ PDFView mediaRectForURL:URL atPage:page ];
// Calculate scale factor
CGFloat scaleFactor = MAX(mediaRect.size.width / size.width, mediaRect.size.height / size.height);
@@ -209,7 +214,12 @@ +(UIImage *) imageWithPDFURL:(NSURL *)URL fitSize:(CGSize)size
+(UIImage *) imageWithPDFURL:(NSURL *)URL atWidth:(CGFloat)width atPage:(int)page
{
- CGRect mediaRect = [ PDFView mediaRectForURL:URL ];
+ if ( URL == nil )
+ {
+ return nil;
+ }
+
+ CGRect mediaRect = [ PDFView mediaRectForURL:URL atPage:page ];
CGFloat aspectRatio = mediaRect.size.width / mediaRect.size.height;
CGSize size = CGSizeMake( width, ceilf( width / aspectRatio ));
@@ -226,7 +236,12 @@ +(UIImage *) imageWithPDFURL:(NSURL *)URL atWidth:(CGFloat)width
+(UIImage *) imageWithPDFURL:(NSURL *)URL atHeight:(CGFloat)height atPage:(int)page
{
- CGRect mediaRect = [ PDFView mediaRectForURL:URL ];
+ if ( URL == nil )
+ {
+ return nil;
+ }
+
+ CGRect mediaRect = [ PDFView mediaRectForURL:URL atPage:page ];
CGFloat aspectRatio = mediaRect.size.width / mediaRect.size.height;
CGSize size = CGSizeMake( ceilf( height * aspectRatio ), height );
@@ -243,7 +258,12 @@ +(UIImage *) imageWithPDFURL:(NSURL *)URL atHeight:(CGFloat)height
+(UIImage *) originalSizeImageWithPDFURL:(NSURL *)URL atPage:(int)page
{
- CGRect mediaRect = [ PDFView mediaRectForURL:URL ];
+ if ( URL == nil )
+ {
+ return nil;
+ }
+
+ CGRect mediaRect = [ PDFView mediaRectForURL:URL atPage:page ];
return [ UIImage imageWithPDFURL:URL atSize:mediaRect.size atPage:page ];
}
Something went wrong with that request. Please try again.