Skip to content
This repository

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

Merged
merged 2 commits into from over 1 year ago

2 participants

Mathijs Kadijk Nigel Timothy Barber
Mathijs Kadijk

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

added some commits September 25, 2012
Mathijs Kadijk Fixed PDFView mediaRectForURL: calls
All calls now request the rect for the correct page.
0bb2426
Mathijs Kadijk Invalid resource path/url will return nil UIImages
before this commit the App just crashed on invalid paths.
078d0be
Nigel Timothy Barber mindbrix merged commit e6ea355 into from September 26, 2012
Nigel Timothy Barber mindbrix closed this September 26, 2012
Nigel Timothy Barber
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

Showing 2 unique commits by 1 author.

Sep 25, 2012
Mathijs Kadijk Fixed PDFView mediaRectForURL: calls
All calls now request the rect for the correct page.
0bb2426
Mathijs Kadijk Invalid resource path/url will return nil UIImages
before this commit the App just crashed on invalid paths.
078d0be
This page is out of date. Refresh to see the latest.
10  UIImage+PDF/PDFView.m 100644 → 100755
@@ -93,7 +93,15 @@ +(int) pageCountForURL:(NSURL *)resourceURL
93 93
 
94 94
 +(NSURL *)resourceURLForName:(NSString *)resourceName
95 95
 {
96  
-    return ( resourceName ) ? [ NSURL fileURLWithPath:[[ NSBundle mainBundle ] pathForResource:resourceName ofType:nil ]] : nil;
  96
+    NSString *path = [[ NSBundle mainBundle ] pathForResource:resourceName ofType:nil ];
  97
+    if( path == nil )
  98
+    {
  99
+        return nil;
  100
+    }
  101
+    else
  102
+    {
  103
+        return ( resourceName ) ? [ NSURL fileURLWithPath:path] : nil;
  104
+    }
97 105
 }
98 106
 
99 107
 
28  UIImage+PDF/UIImage+PDF.m 100644 → 100755
@@ -188,8 +188,13 @@ +(UIImage *) imageWithPDFURL:(NSURL *)URL atSize:(CGSize)size
188 188
 
189 189
 +(UIImage *) imageWithPDFURL:(NSURL *)URL fitSize:(CGSize)size atPage:(int)page
190 190
 {
  191
+    if ( URL == nil )
  192
+    {
  193
+        return nil;
  194
+    }
  195
+    
191 196
     // Get dimensions
192  
-    CGRect mediaRect = [ PDFView mediaRectForURL:URL ];
  197
+    CGRect mediaRect = [ PDFView mediaRectForURL:URL atPage:page ];
193 198
     
194 199
     // Calculate scale factor
195 200
     CGFloat scaleFactor = MAX(mediaRect.size.width / size.width, mediaRect.size.height / size.height);
@@ -209,7 +214,12 @@ +(UIImage *) imageWithPDFURL:(NSURL *)URL fitSize:(CGSize)size
209 214
 
210 215
 +(UIImage *) imageWithPDFURL:(NSURL *)URL atWidth:(CGFloat)width atPage:(int)page
211 216
 {
212  
-    CGRect mediaRect = [ PDFView mediaRectForURL:URL ];
  217
+    if ( URL == nil )
  218
+    {
  219
+        return nil;
  220
+    }
  221
+    
  222
+    CGRect mediaRect = [ PDFView mediaRectForURL:URL atPage:page ];
213 223
     CGFloat aspectRatio = mediaRect.size.width / mediaRect.size.height;
214 224
     
215 225
     CGSize size = CGSizeMake( width, ceilf( width / aspectRatio ));
@@ -226,7 +236,12 @@ +(UIImage *) imageWithPDFURL:(NSURL *)URL atWidth:(CGFloat)width
226 236
 
227 237
 +(UIImage *) imageWithPDFURL:(NSURL *)URL atHeight:(CGFloat)height atPage:(int)page
228 238
 {
229  
-    CGRect mediaRect = [ PDFView mediaRectForURL:URL ];
  239
+    if ( URL == nil )
  240
+    {
  241
+        return nil;
  242
+    }
  243
+    
  244
+    CGRect mediaRect = [ PDFView mediaRectForURL:URL atPage:page ];
230 245
     CGFloat aspectRatio = mediaRect.size.width / mediaRect.size.height;
231 246
     
232 247
     CGSize size = CGSizeMake( ceilf( height * aspectRatio ), height );
@@ -243,7 +258,12 @@ +(UIImage *) imageWithPDFURL:(NSURL *)URL atHeight:(CGFloat)height
243 258
 
244 259
 +(UIImage *) originalSizeImageWithPDFURL:(NSURL *)URL atPage:(int)page
245 260
 {
246  
-    CGRect mediaRect = [ PDFView mediaRectForURL:URL ];
  261
+    if ( URL == nil )
  262
+    {
  263
+        return nil;
  264
+    }
  265
+    
  266
+    CGRect mediaRect = [ PDFView mediaRectForURL:URL atPage:page ];
247 267
     
248 268
     return [ UIImage imageWithPDFURL:URL atSize:mediaRect.size atPage:page ];
249 269
 }
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.