forked from core-plot/core-plot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCPTImagePlatformSpecific.m
More file actions
66 lines (55 loc) · 2.31 KB
/
Copy pathCPTImagePlatformSpecific.m
File metadata and controls
66 lines (55 loc) · 2.31 KB
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
#import "CPTImage.h"
@implementation CPTImage(CPTPlatformSpecificImageExtensions)
#pragma mark -
#pragma mark Init/Dealloc
/** @brief Initializes a CPTImage instance with the provided platform-native image.
*
* @param anImage The platform-native image.
* @return A CPTImage instance initialized with the provided image.
**/
-(instancetype)initWithNativeImage:(CPTNativeImage *)anImage
{
if ( (self = [self init]) ) {
self.nativeImage = anImage;
}
return self;
}
/** @brief Initializes a CPTImage instance with the contents of a PNG file.
*
* On systems that support hi-dpi or @quote{Retina} displays, this method will look for a
* double-resolution image with the given name followed by @quote{@2x}. If the @quote{@2x} image
* is not available, the named image file will be loaded.
*
* @param path The file system path of the file.
* @return A CPTImage instance initialized with the contents of the PNG file.
**/
-(instancetype)initForPNGFile:(NSString *)path
{
CGFloat imageScale = CPTFloat(1.0);
// Try to load @2x file if the system supports hi-dpi display
NSImage *newNativeImage = [[NSImage alloc] init];
NSImageRep *imageRep = nil;
for ( NSScreen *screen in [NSScreen screens] ) {
imageScale = MAX(imageScale, screen.backingScaleFactor);
}
while ( imageScale > CPTFloat(1.0) ) {
NSMutableString *hiDpiPath = [path mutableCopy];
NSUInteger replaceCount = [hiDpiPath replaceOccurrencesOfString:@".png"
withString:[NSString stringWithFormat:@"@%dx.png", (int)imageScale]
options:NSCaseInsensitiveSearch | NSBackwardsSearch | NSAnchoredSearch
range:NSMakeRange(hiDpiPath.length - 4, 4)];
if ( replaceCount == 1 ) {
imageRep = [NSImageRep imageRepWithContentsOfFile:hiDpiPath];
if ( imageRep ) {
[newNativeImage addRepresentation:imageRep];
}
}
imageScale -= CPTFloat(1.0);
}
imageRep = [NSImageRep imageRepWithContentsOfFile:path];
if ( imageRep ) {
[newNativeImage addRepresentation:imageRep];
}
return [self initWithNativeImage:newNativeImage];
}
@end