-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathZennyViewDstImageViewController.m
159 lines (129 loc) · 5.01 KB
/
ZennyViewDstImageViewController.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
//
// ZennyViewDstImageViewController.m
// CPU Dasher
//
// Created by zenny_chen on 13-4-17.
//
//
#import "ZennyViewDstImageViewController.h"
@interface ZennyViewDstImageViewController ()
@end
@implementation ZennyViewDstImageViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)loadView
{
CGRect screenBounds = [UIScreen mainScreen].bounds;
CGFloat totalHeight = screenBounds.size.height - 20.0f;
UIView *aView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, screenBounds.size.width, totalHeight)];
aView.backgroundColor = [UIColor blackColor];
self.view = aView;
[aView release];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
CGFloat height = self.view.bounds.size.height;
CGFloat imageHeight = self.view.bounds.size.width;
CGFloat y = (height - imageHeight) * 0.5f;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, y, self.view.bounds.size.width, imageHeight)];
imageView.backgroundColor = [UIColor clearColor];
imageView.contentScaleFactor = [UIScreen mainScreen].scale;
UIImage *image = [ZennyViewDstImageViewController convertBitmapRGBA8ToUIImage:iImageBuffer withWidth:iImageWidth withHeight:iImageHeight];
imageView.image = image;
[self.view addSubview:imageView];
[imageView release];
UIButton *button = [[UIButton alloc] initWithFrame:self.view.bounds];
button.backgroundColor = [UIColor clearColor];
[button addTarget:self action:@selector(buttonTouched:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[button release];
}
- (void)buttonTouched:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}
+ (UIImage *)convertBitmapRGBA8ToUIImage:(unsigned char*)buffer
withWidth:(int) width
withHeight:(int) height {
size_t bufferLength = width * height * 4;
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer, bufferLength, NULL);
size_t bitsPerComponent = 8;
size_t bitsPerPixel = 32;
size_t bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
if(colorSpaceRef == NULL) {
NSLog(@"Error allocating color space");
CGDataProviderRelease(provider);
return nil;
}
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef iref = CGImageCreate(width,
height,
bitsPerComponent,
bitsPerPixel,
bytesPerRow,
colorSpaceRef,
bitmapInfo,
provider, // data provider
NULL, // decode
YES, // should interpolate
renderingIntent);
uint32_t* pixels = (uint32_t*)malloc(bufferLength);
if(pixels == NULL) {
NSLog(@"Error: Memory not allocated for bitmap");
CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(iref);
return nil;
}
CGContextRef context = CGBitmapContextCreate(pixels,
width,
height,
bitsPerComponent,
bytesPerRow,
colorSpaceRef,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
if(context == NULL) {
NSLog(@"Error context not created");
free(pixels);
pixels = NULL;
}
CGContextTranslateCTM(context, 0.0f, (CGFloat)height);
CGContextScaleCTM(context, 1.0f, -1.0f);
UIImage *image = nil;
if(context) {
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, width, height), iref);
CGImageRef imageRef = CGBitmapContextCreateImage(context);
// Support both iPad 3.2 and iPhone 4 Retina displays with the correct scale
if([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) {
float scale = [[UIScreen mainScreen] scale];
image = [UIImage imageWithCGImage:imageRef scale:scale orientation:UIImageOrientationUp];
} else {
image = [UIImage imageWithCGImage:imageRef];
}
CGImageRelease(imageRef);
CGContextRelease(context);
}
CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(iref);
CGDataProviderRelease(provider);
if(pixels) {
free(pixels);
}
return image;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end