EnkiUtils
Small library of classes that Enki Labs uses internally.
 All Classes Functions Properties Pages
UIImage+EnkiUtils.m
1 //
2 // UIImage+EnkiUtils.m
3 //
4 // portions Copyright (c) 2013 carlj.
5 // portions Copyright (c) 2013 gcamp.
6 //
7 
8 #import "UIImage+EnkiUtils.h"
9 
10 @implementation UIImage (EnkiUtils)
11 
12 // https://gist.github.com/3782351
13 // thanks carlj
14 
15 + (UIImage*)imageNamedForDevice:(NSString*)name {
16 
17  if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
18  {
19  if (([UIScreen mainScreen].bounds.size.height * [UIScreen mainScreen].scale) >= 1136.0f)
20  {
21  //Check if is there a path extension or not
22  if (name.pathExtension.length) {
23  name = [name stringByReplacingOccurrencesOfString: [NSString stringWithFormat:@".%@", name.pathExtension]
24  withString: [NSString stringWithFormat:@"-568h@2x.%@", name.pathExtension ] ];
25 
26  } else {
27  name = [name stringByAppendingString:@"-568h@2x"];
28  }
29 
30  //load the image e.g from disk or cache
31  UIImage *image = [UIImage imageNamed: name ];
32  if (image) {
33  //strange Bug in iOS, the image name have a "@2x" but the scale isn't 2.0f
34  return [UIImage imageWithCGImage: image.CGImage scale:2.0f orientation:image.imageOrientation];
35  }
36 
37  }
38  }
39 
40  return [UIImage imageNamed: name ];
41 
42 }
43 
44 // See http://stackoverflow.com/questions/2025319/scale-image-in-an-uibutton-to-aspectfit,
45 // Thanks gcamp
46 //
47 - (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize
48 {
49 
50  UIImage *sourceImage = self;
51  UIImage *newImage = nil;
52 
53  CGSize imageSize = sourceImage.size;
54  CGFloat width = imageSize.width;
55  CGFloat height = imageSize.height;
56 
57  CGFloat targetWidth = targetSize.width;
58  CGFloat targetHeight = targetSize.height;
59 
60  CGFloat scaleFactor = 0.0;
61  CGFloat scaledWidth = targetWidth;
62  CGFloat scaledHeight = targetHeight;
63 
64  CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
65 
66  if (!CGSizeEqualToSize(imageSize, targetSize)) {
67 
68  CGFloat widthFactor = targetWidth / width;
69  CGFloat heightFactor = targetHeight / height;
70 
71  if (widthFactor < heightFactor)
72  scaleFactor = widthFactor;
73  else
74  scaleFactor = heightFactor;
75 
76  scaledWidth = width * scaleFactor;
77  scaledHeight = height * scaleFactor;
78 
79  // center the image
80 
81  if (widthFactor < heightFactor) {
82  thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
83  } else if (widthFactor > heightFactor) {
84  thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
85  }
86  }
87 
88 
89  // this is actually the interesting part:
90 
91  UIGraphicsBeginImageContext(targetSize);
92 
93  CGRect thumbnailRect = CGRectZero;
94  thumbnailRect.origin = thumbnailPoint;
95  thumbnailRect.size.width = scaledWidth;
96  thumbnailRect.size.height = scaledHeight;
97 
98  [sourceImage drawInRect:thumbnailRect];
99 
100  newImage = UIGraphicsGetImageFromCurrentImageContext();
101  UIGraphicsEndImageContext();
102 
103  if(newImage == nil) {
104  NSLog(@"could not scale image");
105  }
106 
107  return newImage ;
108 }
109 
110 
111 @end