-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageRenderOperation.m
119 lines (94 loc) · 3.42 KB
/
ImageRenderOperation.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//
// ImageRenderOperation.m
// Trianglam
//
// Created by Petr Zvoníček on 07.08.13.
// Copyright (c) 2013 X Production s.r.o. All rights reserved.
//
#import "ImageRenderOperation.h"
@interface ImageRenderOperation()
@end
@implementation ImageRenderOperation
-(id)initWithImage:(NSString*)path delegate:(id<ImageRenderDelegate>)delegate index:(NSInteger)index andImageView:(UIImageView *)imageView;
{
if (self = [super init]) {
self.delegate = delegate;
self.path = path;
self.index = index;
self.imageView = imageView;
}
return self;
}
- (void)main {
CGDataProviderRef imageDataProvider = CGDataProviderCreateWithFilename([_path fileSystemRepresentation]);
CGImageRef image = CGImageCreateWithJPEGDataProvider(imageDataProvider, NULL, NO, kCGRenderingIntentDefault);
if (self.isCancelled)
{
CGImageRelease(image);
CGDataProviderRelease(imageDataProvider);
return;
}
// Create a bitmap context from the image's specifications
// (Note: We need to specify kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little
// because PNGs are optimized by Xcode this way.)
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef bitmapContext = CGBitmapContextCreate(NULL, CGImageGetWidth(image), CGImageGetHeight(image), CGImageGetBitsPerComponent(image), CGImageGetWidth(image) * 4, colorSpace, kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
CGContextSetInterpolationQuality(bitmapContext, kCGInterpolationNone);
if (self.isCancelled)
{
CGImageRelease(image);
CGDataProviderRelease(imageDataProvider);
CGContextRelease(bitmapContext);
CGColorSpaceRelease(colorSpace);
return;
}
// Draw the image into the bitmap context and retrieve the
// decompressed image
CGContextDrawImage(bitmapContext, CGRectMake(0, 0, CGImageGetWidth(image), CGImageGetHeight(image)), image);
CGImageRef decompressedImage = CGBitmapContextCreateImage(bitmapContext);
if (self.isCancelled)
{
CGImageRelease(decompressedImage);
CGContextRelease(bitmapContext);
CGColorSpaceRelease(colorSpace);
CGImageRelease(image);
CGDataProviderRelease(imageDataProvider);
return;
}
// Create a UIImage
self.renderedImage = [[UIImage alloc] initWithCGImage:decompressedImage];
if (self.isCancelled)
{
CGImageRelease(decompressedImage);
CGContextRelease(bitmapContext);
CGColorSpaceRelease(colorSpace);
CGImageRelease(image);
CGDataProviderRelease(imageDataProvider);
return;
}
// Release everything
CGImageRelease(decompressedImage);
CGContextRelease(bitmapContext);
CGColorSpaceRelease(colorSpace);
CGImageRelease(image);
CGDataProviderRelease(imageDataProvider);
if (self.isCancelled)
return;
[(NSObject *)self.delegate performSelectorOnMainThread:@selector(renderDidFinish:) withObject:self waitUntilDone:NO];
}
-(BOOL)isEqual:(id)other
{
if (other == self)
return YES;
if (!other || ![other isKindOfClass:[self class]])
return NO;
return self.index == ((ImageRenderOperation*)other).index;
}
-(NSUInteger)hash
{
NSUInteger prime = 31;
NSUInteger result = 1;
result = prime * result + self.index;
return result;
}
@end