-
Notifications
You must be signed in to change notification settings - Fork 0
/
UIImage+VJDeviceSpecialMedia.m
95 lines (81 loc) · 2.72 KB
/
UIImage+VJDeviceSpecialMedia.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//
// UIImage+VJDeviceSpecialMedia.m
// VJUIImageDeviceSpecificMedia
//
// Created by Victor Jiang on 3/3/15.
// Copyright (c) 2015 Victor Jiang. All rights reserved.
//
#import "UIImage+VJDeviceSpecialMedia.h"
VJDeviceClass VJ_CurrentDeviceClass()
{
CGFloat greaterPixelDimension = fmaxf([[UIScreen mainScreen] bounds].size.height, [[UIScreen mainScreen] bounds].size.width);
switch ((NSInteger)greaterPixelDimension) {
case 480:
return ([[UIScreen mainScreen] scale] > 1.0) ? VJDeviceClass_iPhoneRetina : VJDeviceClass_iPhone;
break;
case 568:
return VJDeviceClass_iPhone5;
break;
case 667:
return VJDeviceClass_iPhone6;
break;
case 736:
return VJDeviceClass_iPhone6plus;
break;
case 1024:
return ([[UIScreen mainScreen] scale] > 1.0) ? VJDeviceClass_iPadRetina : VJDeviceClass_iPad;
break;
default:
return VJDeviceClass_unKnown;
break;
}
}
@implementation UIImage (VJDeviceSpecialMedia)
+ (instancetype)vj_imageForDeviceWithName:(NSString *)imageName
{
return [UIImage vj_imageForDeviceWithName:imageName type:@"png"];
}
+ (instancetype)vj_imageForDeviceWithName:(NSString *)imageName type:(NSString *)type
{
NSString *suffixString;
switch (VJ_CurrentDeviceClass()) {
case VJDeviceClass_iPhone:
suffixString = @"";
break;
case VJDeviceClass_iPhoneRetina:
suffixString = @"@2x";
break;
case VJDeviceClass_iPhone5:
suffixString = @"-568h@2x";
break;
case VJDeviceClass_iPhone6:
suffixString = @"-667h@2x";
break;
case VJDeviceClass_iPhone6plus:
suffixString = @"-736h@3x";
break;
case VJDeviceClass_iPad:
suffixString = @"~ipad";
break;
case VJDeviceClass_iPadRetina:
suffixString = @"~ipad@2x";
break;
case VJDeviceClass_unKnown:
default:
suffixString = @"";
break;
}
UIImage *image = nil;
NSString *imageFullName = [imageName stringByAppendingString:suffixString];
// if type is not png, imageFullName & imageName will append the suffix of type
if (![[type lowercaseString] isEqualToString:@"png"]) {
imageFullName = [[imageFullName stringByAppendingString:@"."] stringByAppendingString:type];
imageName = [[imageName stringByAppendingString:@"."] stringByAppendingString:type];
}
image = [UIImage imageNamed:imageFullName];
if (!image) {
image = [UIImage imageNamed:imageName];
}
return image;
}
@end