-
Notifications
You must be signed in to change notification settings - Fork 4
/
NSImage-Extras.m
40 lines (30 loc) · 979 Bytes
/
NSImage-Extras.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
//
// NSImage-Extras.m
//
// Created by Scott Stevenson on 9/28/07.
// Source may be reused with virtually no restriction. See License.txt
#import "NSImage-Extras.h"
#import <QuartzCore/QuartzCore.h>
@implementation NSImage (Extras)
- (CGImageRef)cgImage
{
CGImageRef image = CreateCGImageFromData([self TIFFRepresentation]);
if (image)
CFMakeCollectable(image);
return image;
}
@end
// from http://developer.apple.com/technotes/tn2005/tn2143.html
CGImageRef CreateCGImageFromData(NSData* data)
{
CGImageRef imageRef = NULL;
CGImageSourceRef sourceRef;
sourceRef = CGImageSourceCreateWithData((CFDataRef)data, NULL);
if(sourceRef) {
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:NO]
forKey:(NSString *)kCGImageSourceShouldCache];
imageRef = CGImageSourceCreateImageAtIndex(sourceRef, 0, (CFDictionaryRef)options);
CFRelease(sourceRef);
}
return imageRef;
}