Skip to content

Commit 8ebfae5

Browse files
committed
Complete set of NSData methods
1 parent bbe64f7 commit 8ebfae5

1 file changed

Lines changed: 74 additions & 2 deletions

File tree

UIImage+PDF/UIImage+PDF.m

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,17 +202,88 @@ +(UIImage *) originalSizeImageWithPDFNamed:(NSString *)resourceName
202202
return [ self originalSizeImageWithPDFURL:[ PDFView resourceURLForName:resourceName ]];
203203
}
204204

205+
206+
#pragma mark - Resource Data
207+
205208
+(UIImage *) originalSizeImageWithPDFData:(NSData *)data
206209
{
207210
CGRect mediaRect = [ PDFView mediaRectForData:data atPage:1 ];
208211

209212
return [ UIImage imageWithPDFData:data atSize:mediaRect.size atPage:1 ];
210213
}
211214

212-
#pragma mark - Resource URLs
213215

214-
+(UIImage *) imageWithPDFData:(NSData *)data atSize:(CGSize)size atPage:(int)page
216+
+(UIImage *)imageWithPDFData:(NSData *)data atWidth:(CGFloat)width
217+
{
218+
return [ UIImage imageWithPDFData:data atWidth:width atPage:1 ];
219+
}
220+
221+
+(UIImage *)imageWithPDFData:(NSData *)data atWidth:(CGFloat)width atPage:(int)page
222+
{
223+
if ( data == nil )
224+
{
225+
return nil;
226+
}
227+
228+
CGRect mediaRect = [ PDFView mediaRectForData:data atPage:page ];
229+
CGFloat aspectRatio = mediaRect.size.width / mediaRect.size.height;
230+
CGSize size = CGSizeMake( width, ceilf( width / aspectRatio ));
231+
232+
return [ UIImage imageWithPDFData:data atSize:size atPage:page ];
233+
}
234+
235+
+(UIImage *)imageWithPDFData:(NSData *)data atHeight:(CGFloat)height
215236
{
237+
return [ UIImage imageWithPDFData:data atHeight:height atPage:1 ];
238+
}
239+
240+
+(UIImage *)imageWithPDFData:(NSData *)data atHeight:(CGFloat)height atPage:(int)page
241+
{
242+
if ( data == nil )
243+
{
244+
return nil;
245+
}
246+
247+
CGRect mediaRect = [ PDFView mediaRectForData:data atPage:page ];
248+
CGFloat aspectRatio = mediaRect.size.width / mediaRect.size.height;
249+
CGSize size = CGSizeMake( ceilf( height * aspectRatio ), height );
250+
251+
return [ UIImage imageWithPDFData:data atSize:size atPage:page ];
252+
}
253+
254+
+(UIImage *)imageWithPDFData:(NSData *)data fitSize:(CGSize)size
255+
{
256+
return [ UIImage imageWithPDFData:data fitSize:size atPage:1 ];
257+
}
258+
259+
+(UIImage *)imageWithPDFData:(NSData *)data fitSize:(CGSize)size atPage:(int)page
260+
{
261+
if ( data == nil )
262+
{
263+
return nil;
264+
}
265+
266+
CGRect mediaRect = [ PDFView mediaRectForData:data atPage:page ];
267+
CGFloat scaleFactor = MAX( mediaRect.size.width / size.width, mediaRect.size.height / size.height );
268+
CGSize newSize = CGSizeMake( ceilf( mediaRect.size.width / scaleFactor ), ceilf( mediaRect.size.height / scaleFactor ));
269+
270+
// Return image
271+
return [ UIImage imageWithPDFData:data atSize:newSize atPage:page ];
272+
}
273+
274+
275+
+(UIImage *)imageWithPDFData:(NSData *)data atSize:(CGSize)size
276+
{
277+
return [ UIImage imageWithPDFData:data atSize:size atPage:1 ];
278+
}
279+
280+
+(UIImage *)imageWithPDFData:(NSData *)data atSize:(CGSize)size atPage:(int)page
281+
{
282+
if ( data == nil )
283+
{
284+
return nil;
285+
}
286+
216287
UIImage *pdfImage = nil;
217288

218289
NSString *cacheFilename = [ self cacheFilenameForData:data atSize:size atScaleFactor:[ UIScreen mainScreen ].scale atPage:page ];
@@ -243,6 +314,7 @@ +(UIImage *) imageWithPDFData:(NSData *)data atSize:(CGSize)size atPage:(int)pag
243314
}
244315

245316

317+
#pragma mark - Resource URLs
246318

247319
+(UIImage *) imageWithPDFURL:(NSURL *)URL atSize:(CGSize)size atPage:(int)page
248320
{

0 commit comments

Comments
 (0)