8 #import "UIImage+EnkiUtils.h"
15 + (UIImage*)imageNamedForDevice:(NSString*)name {
17 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
19 if (([UIScreen mainScreen].bounds.size.height * [UIScreen mainScreen].scale) >= 1136.0f)
22 if (name.pathExtension.length) {
23 name = [name stringByReplacingOccurrencesOfString: [NSString stringWithFormat:@".%@", name.pathExtension]
24 withString: [NSString stringWithFormat:@"-568h@2x.%@", name.pathExtension ] ];
27 name = [name stringByAppendingString:@"-568h@2x"];
31 UIImage *image = [UIImage imageNamed: name ];
34 return [UIImage imageWithCGImage: image.CGImage scale:2.0f orientation:image.imageOrientation];
40 return [UIImage imageNamed: name ];
47 - (UIImage *)imageByScalingProportionallyToSize:(CGSize)targetSize
50 UIImage *sourceImage =
self;
51 UIImage *newImage = nil;
53 CGSize imageSize = sourceImage.size;
54 CGFloat width = imageSize.width;
55 CGFloat height = imageSize.height;
57 CGFloat targetWidth = targetSize.width;
58 CGFloat targetHeight = targetSize.height;
60 CGFloat scaleFactor = 0.0;
61 CGFloat scaledWidth = targetWidth;
62 CGFloat scaledHeight = targetHeight;
64 CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
66 if (!CGSizeEqualToSize(imageSize, targetSize)) {
68 CGFloat widthFactor = targetWidth / width;
69 CGFloat heightFactor = targetHeight / height;
71 if (widthFactor < heightFactor)
72 scaleFactor = widthFactor;
74 scaleFactor = heightFactor;
76 scaledWidth = width * scaleFactor;
77 scaledHeight = height * scaleFactor;
81 if (widthFactor < heightFactor) {
82 thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
83 }
else if (widthFactor > heightFactor) {
84 thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
91 UIGraphicsBeginImageContext(targetSize);
93 CGRect thumbnailRect = CGRectZero;
94 thumbnailRect.origin = thumbnailPoint;
95 thumbnailRect.size.width = scaledWidth;
96 thumbnailRect.size.height = scaledHeight;
98 [sourceImage drawInRect:thumbnailRect];
100 newImage = UIGraphicsGetImageFromCurrentImageContext();
101 UIGraphicsEndImageContext();
103 if(newImage == nil) {
104 NSLog(
@"could not scale image");