-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCGImage+TLExtensions.c
More file actions
100 lines (93 loc) · 3.17 KB
/
Copy pathCGImage+TLExtensions.c
File metadata and controls
100 lines (93 loc) · 3.17 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
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
96
97
98
99
100
/*
* CGImage+TLExtensions.c
* Flowrate
*
* Created by Nathan Vander Wilt on 1/8/10.
* Copyright 2010 Calf Trail Software, LLC. All rights reserved.
*
*/
#include "CGImage+TLExtensions.h"
#include "CFType+TLExtensions.h"
CGImageRef tlCGImageCreateWithURL(CFURLRef imageURL) {
CGImageSourceRef isrc = CGImageSourceCreateWithURL(imageURL, NULL);
CGImageRef img = NULL;
if (isrc) {
if (CGImageSourceGetCount(isrc)) {
img = CGImageSourceCreateImageAtIndex(isrc, 0, NULL);
}
CFRelease(isrc);
}
return img;
}
CGImageRef tlCGImageGet(CFStringRef imgName) {
CFArrayRef pieces = CFStringCreateArrayBySeparatingStrings(kCFAllocatorDefault,
imgName, CFSTR("."));
CFIndex numPieces = CFArrayGetCount(pieces);
CFStringRef extension = NULL;
if (numPieces > 1) {
extension = CFArrayGetValueAtIndex(pieces, numPieces-1);
CFMutableArrayRef namePieces = CFArrayCreateMutableCopy(kCFAllocatorDefault, 0, pieces);
CFArrayRemoveValueAtIndex(namePieces, numPieces-1);
imgName = CFStringCreateByCombiningStrings(kCFAllocatorDefault, namePieces, CFSTR("."));
CFRelease(namePieces);
}
else {
CFRetain(imgName);
}
CFRelease(pieces);
CFURLRef imageURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), imgName, extension, NULL);
CGImageRef img = tlCGImageCreateWithURL(imageURL);
CFRelease(imageURL);
CFRelease(imgName);
return img ? (CGImageRef)tlCFAutoreleaseC(img) : NULL;
}
CGImageRef tlCGImageMakeFromURL(CFURLRef imageURL, size_t maxImageSize) {
CGImageRef img = NULL;
CGImageSourceRef imgSrc = CGImageSourceCreateWithURL(imageURL, NULL);
if (imgSrc) {
if (CGImageSourceGetCount(imgSrc)) {
if (!maxImageSize) {
CFDictionaryRef imgProps = CGImageSourceCopyPropertiesAtIndex(imgSrc, 0, NULL);
if (imgProps) {
CFNumberRef w = CFDictionaryGetValue(imgProps, kCGImagePropertyPixelWidth);
CFNumberRef h = CFDictionaryGetValue(imgProps, kCGImagePropertyPixelHeight);
if (w && h) {
CFComparisonResult cmp = CFNumberCompare(w, h, NULL);
switch (cmp) {
case kCFCompareLessThan:
case kCFCompareEqualTo:
CFNumberGetValue(h, kCFNumberLongType, &maxImageSize);
break;
case kCFCompareGreaterThan:
CFNumberGetValue(w, kCFNumberLongType, &maxImageSize);
break;
}
}
CFRelease(imgProps);
}
}
if (maxImageSize) {
CFStringRef optionKeys[] = {
kCGImageSourceCreateThumbnailFromImageAlways,
kCGImageSourceCreateThumbnailWithTransform,
kCGImageSourceThumbnailMaxPixelSize
};
CFNumberRef maxSizeNum = CFNumberCreate(kCFAllocatorDefault, kCFNumberLongType, &maxImageSize);
CFTypeRef optionValues[] = {
kCFBooleanTrue,
kCFBooleanTrue,
maxSizeNum
};
CFDictionaryRef thumbnailOptions = CFDictionaryCreate(kCFAllocatorDefault,
(const void**)optionKeys, (const void**)optionValues, 3,
&kCFCopyStringDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFRelease(maxSizeNum);
img = CGImageSourceCreateThumbnailAtIndex(imgSrc, 0, thumbnailOptions);
CFRelease(thumbnailOptions);
}
}
CFRelease(imgSrc);
}
return img ? (CGImageRef)tlCFAutoreleaseC(img) : NULL;
}