forked from rnystrom/RNBlurModalView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RNBlurModalView.m
694 lines (539 loc) · 22.3 KB
/
RNBlurModalView.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/*
* RNBlurModal
*
* Created by Ryan Nystrom on 10/2/12.
* Copyright (c) 2012 Ryan Nystrom. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#import "RNBlurModalView.h"
#import <Accelerate/Accelerate.h>
#import <QuartzCore/QuartzCore.h>
/*
This bit is important! In order to prevent capturing selected states of UIResponders I've implemented a delay. Please feel free to set this delay to *whatever* you deem apprpriate.
I've defaulted it to 0.125 seconds. You can do shorter/longer as you see fit.
*/
CGFloat const kRNBlurDefaultDelay = 0.125f;
/*
You can also change this constant to make the blur more "blurry". I recommend the tasteful level of 0.2 and no higher. However, you are free to change this from 0.0 to 1.0.
*/
CGFloat const kRNDefaultBlurScale = 0.2f;
CGFloat const kRNBlurDefaultDuration = 0.2f;
CGFloat const kRNBlurViewMaxAlpha = 1.f;
CGFloat const kRNBlurBounceOutDurationScale = 0.8f;
NSString * const kRNBlurDidShowNotification = @"com.whoisryannystrom.RNBlurModalView.show";
NSString * const kRNBlurDidHidewNotification = @"com.whoisryannystrom.RNBlurModalView.hide";
typedef void (^RNBlurCompletion)(void);
@interface UILabel (AutoSize)
- (void)autoHeight;
@end
@interface UIView (Sizes)
@property (nonatomic) CGFloat left;
@property (nonatomic) CGFloat top;
@property (nonatomic) CGFloat right;
@property (nonatomic) CGFloat bottom;
@property (nonatomic) CGFloat width;
@property (nonatomic) CGFloat height;
@property (nonatomic) CGPoint origin;
@property (nonatomic) CGSize size;
@end
@interface UIView (Screenshot)
- (UIImage*)screenshot;
@end
@interface UIImage (Blur)
-(UIImage *)boxblurImageWithBlur:(CGFloat)blur;
@end
@interface RNBlurView : UIImageView
- (id)initWithCoverView:(UIView*)view;
@end
@interface RNCloseButton : UIButton
@end
@interface RNBlurModalView ()
@property (assign, readwrite) BOOL isVisible;
@end
#pragma mark - RNBlurModalView
@implementation RNBlurModalView {
UIViewController *_controller;
UIView *_parentView;
UIView *_contentView;
RNCloseButton *_dismissButton;
RNBlurView *_blurView;
RNBlurCompletion _completion;
}
+ (UIView*)generateModalViewWithTitle:(NSString*)title message:(NSString*)message {
CGFloat defaultWidth = 280.f;
CGRect frame = CGRectMake(0, 0, defaultWidth, 0);
CGFloat padding = 10.f;
UIView *view = [[UIView alloc] initWithFrame:frame];
UIColor *whiteColor = [UIColor colorWithRed:0.816 green:0.788 blue:0.788 alpha:1.000];
view.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.8f];
view.layer.borderColor = whiteColor.CGColor;
view.layer.borderWidth = 2.f;
view.layer.cornerRadius = 10.f;
UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(padding, 0, defaultWidth - padding * 2.f, 0)];
titleLabel.text = title;
titleLabel.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:17.f];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.shadowColor = [UIColor blackColor];
titleLabel.shadowOffset = CGSizeMake(0, -1);
titleLabel.textAlignment = NSTextAlignmentCenter;
titleLabel.backgroundColor = [UIColor clearColor];
[titleLabel autoHeight];
titleLabel.top = padding;
[view addSubview:titleLabel];
UILabel *messageLabel = [[UILabel alloc] initWithFrame:CGRectMake(padding, 0, defaultWidth - padding * 2.f, 0)];
messageLabel.text = message;
messageLabel.numberOfLines = 0;
messageLabel.font = [UIFont fontWithName:@"HelveticaNeue" size:17.f];
messageLabel.textColor = titleLabel.textColor;
messageLabel.shadowOffset = titleLabel.shadowOffset;
messageLabel.shadowColor = titleLabel.shadowColor;
messageLabel.textAlignment = NSTextAlignmentCenter;
messageLabel.backgroundColor = [UIColor clearColor];
[messageLabel autoHeight];
messageLabel.top = titleLabel.bottom + padding;
[view addSubview:messageLabel];
view.height = messageLabel.bottom + padding;
view.autoresizingMask = UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin;
return view;
}
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
_dismissButton = [[RNCloseButton alloc] init];
_dismissButton.center = CGPointZero;
[_dismissButton addTarget:self action:@selector(hide) forControlEvents:UIControlEventTouchUpInside];
self.alpha = 0.f;
self.backgroundColor = [UIColor clearColor];
// self.backgroundColor = [UIColor redColor];
// self.layer.borderWidth = 2.f;
// self.layer.borderColor = [UIColor blackColor].CGColor;
self.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleLeftMargin |
UIViewAutoresizingFlexibleTopMargin);
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChangeNotification:) name:UIApplicationDidChangeStatusBarOrientationNotification object:nil];
}
return self;
}
- (id)initWithViewController:(UIViewController*)viewController view:(UIView*)view {
if (self = [self initWithFrame:CGRectMake(0, 0, viewController.view.width, viewController.view.height)]) {
[self addSubview:view];
_contentView = view;
_contentView.center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
_controller = viewController;
_parentView = nil;
_contentView.clipsToBounds = YES;
_contentView.layer.masksToBounds = YES;
_dismissButton.center = CGPointMake(view.left, view.top);
[self addSubview:_dismissButton];
}
return self;
}
- (id)initWithViewController:(UIViewController*)viewController title:(NSString*)title message:(NSString*)message {
UIView *view = [RNBlurModalView generateModalViewWithTitle:title message:message];
if (self = [self initWithViewController:viewController view:view]) {
// nothing to see here
}
return self;
}
- (id)initWithParentView:(UIView*)parentView view:(UIView*)view {
if (self = [self initWithFrame:CGRectMake(0, 0, parentView.width, parentView.height)]) {
[self addSubview:view];
_contentView = view;
_contentView.center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
_controller = nil;
_parentView = parentView;
_contentView.clipsToBounds = YES;
_contentView.layer.masksToBounds = YES;
_dismissButton.center = CGPointMake(view.left, view.top);
[self addSubview:_dismissButton];
}
return self;
}
- (id)initWithParentView:(UIView*)parentView title:(NSString*)title message:(NSString*)message {
UIView *view = [RNBlurModalView generateModalViewWithTitle:title message:message];
if (self = [self initWithParentView:parentView view:view]) {
// nothing to see here
}
return self;
}
- (id)initWithView:(UIView*)view {
if (self = [self initWithParentView:[[UIApplication sharedApplication].delegate window].rootViewController.view view:view]) {
// nothing to see here
}
return self;
}
- (id)initWithTitle:(NSString*)title message:(NSString*)message {
UIView *view = [RNBlurModalView generateModalViewWithTitle:title message:message];
if (self = [self initWithView:view]) {
// nothing to see here
}
return self;
}
- (void)layoutSubviews {
[super layoutSubviews];
_dismissButton.center = CGPointMake(_contentView.left, _contentView.top);
}
- (void)willMoveToSuperview:(UIView *)newSuperview {
[super willMoveToSuperview:newSuperview];
if (newSuperview) {
self.center = CGPointMake(CGRectGetMidX(newSuperview.frame), CGRectGetMidY(newSuperview.frame));
}
}
- (void)orientationDidChangeNotification:(NSNotification*)notification {
[self performSelector:@selector(updateSubviews) withObject:nil afterDelay:0.3f];
}
- (void)updateSubviews {
self.hidden = YES;
// get new screenshot after orientation
[_blurView removeFromSuperview]; _blurView = nil;
if (_controller) {
_blurView = [[RNBlurView alloc] initWithCoverView:_controller.view];
_blurView.alpha = 1.f;
[_controller.view insertSubview:_blurView belowSubview:self];
}
else if(_parentView) {
_blurView = [[RNBlurView alloc] initWithCoverView:_parentView];
_blurView.alpha = 1.f;
[_parentView insertSubview:_blurView belowSubview:self];
}
self.hidden = NO;
_contentView.center = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
_dismissButton.center = _contentView.origin;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)show {
[self showWithDuration:kRNBlurDefaultDuration delay:0 options:kNilOptions completion:NULL];
}
- (void)showWithDuration:(CGFloat)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options completion:(void (^)(void))completion {
self.animationDuration = duration;
self.animationDelay = delay;
self.animationOptions = options;
_completion = [completion copy];
// delay so we dont get button states
[self performSelector:@selector(delayedShow) withObject:nil afterDelay:kRNBlurDefaultDelay];
}
- (void)delayedShow {
if (! self.isVisible) {
if (! self.superview) {
if (_controller) {
self.frame = CGRectMake(0, 0, _controller.view.bounds.size.width, _controller.view.bounds.size.height);
[_controller.view addSubview:self];
}
else if(_parentView) {
self.frame = CGRectMake(0, 0, _parentView.bounds.size.width, _parentView.bounds.size.height);
[_parentView addSubview:self];
}
self.top = 0;
}
if (_controller) {
_blurView = [[RNBlurView alloc] initWithCoverView:_controller.view];
_blurView.alpha = 0.f;
self.frame = CGRectMake(0, 0, _controller.view.bounds.size.width, _controller.view.bounds.size.height);
[_controller.view insertSubview:_blurView belowSubview:self];
}
else if(_parentView) {
_blurView = [[RNBlurView alloc] initWithCoverView:_parentView];
_blurView.alpha = 0.f;
self.frame = CGRectMake(0, 0, _parentView.bounds.size.width, _parentView.bounds.size.height);
[_parentView insertSubview:_blurView belowSubview:self];
}
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.4, 0.4);
[UIView animateWithDuration:self.animationDuration animations:^{
_blurView.alpha = 1.f;
self.alpha = 1.f;
self.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.f, 1.f);
} completion:^(BOOL finished) {
if (finished) {
[[NSNotificationCenter defaultCenter] postNotificationName:kRNBlurDidShowNotification object:nil];
self.isVisible = YES;
if (_completion) {
_completion();
}
}
}];
}
}
- (void)hide {
[self hideWithDuration:kRNBlurDefaultDuration delay:0 options:kNilOptions completion:NULL];
}
- (void)hideWithDuration:(CGFloat)duration delay:(NSTimeInterval)delay options:(UIViewAnimationOptions)options completion:(void (^)(void))completion {
if (self.isVisible) {
[UIView animateWithDuration:duration
delay:delay
options:options
animations:^{
self.alpha = 0.f;
_blurView.alpha = 0.f;
}
completion:^(BOOL finished){
if (finished) {
[_blurView removeFromSuperview];
_blurView = nil;
[self removeFromSuperview];
[[NSNotificationCenter defaultCenter] postNotificationName:kRNBlurDidHidewNotification object:nil];
self.isVisible = NO;
if (completion) {
completion();
}
}
}];
}
}
@end
#pragma mark - RNBlurView
@implementation RNBlurView {
UIView *_coverView;
}
- (id)initWithCoverView:(UIView *)view {
if (self = [super initWithFrame:CGRectMake(0, 0, view.bounds.size.width, view.bounds.size.height)]) {
_coverView = view;
UIImage *blur = [_coverView screenshot];
self.image = [blur boxblurImageWithBlur:kRNDefaultBlurScale];
}
return self;
}
@end
#pragma mark - UILabel + Autosize
@implementation UILabel (AutoSize)
- (void)autoHeight {
CGRect frame = self.frame;
CGSize maxSize = CGSizeMake(frame.size.width, 9999);
CGSize expectedSize = [self.text sizeWithFont:self.font constrainedToSize:maxSize lineBreakMode:self.lineBreakMode];
frame.size.height = expectedSize.height;
[self setFrame:frame];
}
@end
#pragma mark - UIView + Sizes
@implementation UIView (Sizes)
- (CGFloat)left {
return self.frame.origin.x;
}
- (void)setLeft:(CGFloat)x {
CGRect frame = self.frame;
frame.origin.x = x;
self.frame = frame;
}
- (CGFloat)top {
return self.frame.origin.y;
}
- (void)setTop:(CGFloat)y {
CGRect frame = self.frame;
frame.origin.y = y;
self.frame = frame;
}
- (CGFloat)right {
return self.frame.origin.x + self.frame.size.width;
}
- (void)setRight:(CGFloat)right {
CGRect frame = self.frame;
frame.origin.x = right - frame.size.width;
self.frame = frame;
}
- (CGFloat)bottom {
return self.frame.origin.y + self.frame.size.height;
}
- (void)setBottom:(CGFloat)bottom {
CGRect frame = self.frame;
frame.origin.y = bottom - frame.size.height;
self.frame = frame;
}
- (CGFloat)width {
return self.frame.size.width;
}
- (void)setWidth:(CGFloat)width {
CGRect frame = self.frame;
frame.size.width = width;
self.frame = frame;
}
- (CGFloat)height {
return self.frame.size.height;
}
- (void)setHeight:(CGFloat)height {
CGRect frame = self.frame;
frame.size.height = height;
self.frame = frame;
}
- (CGPoint)origin {
return self.frame.origin;
}
- (void)setOrigin:(CGPoint)origin {
CGRect frame = self.frame;
frame.origin = origin;
self.frame = frame;
}
- (CGSize)size {
return self.frame.size;
}
- (void)setSize:(CGSize)size {
CGRect frame = self.frame;
frame.size = size;
self.frame = frame;
}
@end
#pragma mark - RNCloseButton
@implementation RNCloseButton
- (id)init{
if(!(self = [super initWithFrame:(CGRect){0, 0, 32, 32}])){
return nil;
}
static UIImage *closeButtonImage;
static dispatch_once_t once;
dispatch_once(&once, ^{
closeButtonImage = [self closeButtonImage];
});
[self setBackgroundImage:closeButtonImage forState:UIControlStateNormal];
return self;
}
- (UIImage *)closeButtonImage{
UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, 0);
//// General Declarations
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = UIGraphicsGetCurrentContext();
//// Color Declarations
UIColor *topGradient = [UIColor colorWithRed:0.21 green:0.21 blue:0.21 alpha:0.9];
UIColor *bottomGradient = [UIColor colorWithRed:0.03 green:0.03 blue:0.03 alpha:0.9];
//// Gradient Declarations
NSArray *gradientColors = @[(id)topGradient.CGColor,
(id)bottomGradient.CGColor];
CGFloat gradientLocations[] = {0, 1};
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef)gradientColors, gradientLocations);
//// Shadow Declarations
CGColorRef shadow = [UIColor blackColor].CGColor;
CGSize shadowOffset = CGSizeMake(0, 1);
CGFloat shadowBlurRadius = 3;
CGColorRef shadow2 = [UIColor blackColor].CGColor;
CGSize shadow2Offset = CGSizeMake(0, 1);
CGFloat shadow2BlurRadius = 0;
//// Oval Drawing
UIBezierPath *ovalPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(4, 3, 24, 24)];
CGContextSaveGState(context);
[ovalPath addClip];
CGContextDrawLinearGradient(context, gradient, CGPointMake(16, 3), CGPointMake(16, 27), 0);
CGContextRestoreGState(context);
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, shadowOffset, shadowBlurRadius, shadow);
[[UIColor whiteColor] setStroke];
ovalPath.lineWidth = 2;
[ovalPath stroke];
CGContextRestoreGState(context);
//// Bezier Drawing
UIBezierPath *bezierPath = [UIBezierPath bezierPath];
[bezierPath moveToPoint:CGPointMake(22.36, 11.46)];
[bezierPath addLineToPoint:CGPointMake(18.83, 15)];
[bezierPath addLineToPoint:CGPointMake(22.36, 18.54)];
[bezierPath addLineToPoint:CGPointMake(19.54, 21.36)];
[bezierPath addLineToPoint:CGPointMake(16, 17.83)];
[bezierPath addLineToPoint:CGPointMake(12.46, 21.36)];
[bezierPath addLineToPoint:CGPointMake(9.64, 18.54)];
[bezierPath addLineToPoint:CGPointMake(13.17, 15)];
[bezierPath addLineToPoint:CGPointMake(9.64, 11.46)];
[bezierPath addLineToPoint:CGPointMake(12.46, 8.64)];
[bezierPath addLineToPoint:CGPointMake(16, 12.17)];
[bezierPath addLineToPoint:CGPointMake(19.54, 8.64)];
[bezierPath addLineToPoint:CGPointMake(22.36, 11.46)];
[bezierPath closePath];
CGContextSaveGState(context);
CGContextSetShadowWithColor(context, shadow2Offset, shadow2BlurRadius, shadow2);
[[UIColor whiteColor] setFill];
[bezierPath fill];
CGContextRestoreGState(context);
//// Cleanup
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
@end
#pragma mark - UIView + Screenshot
@implementation UIView (Screenshot)
- (UIImage*)screenshot {
UIGraphicsBeginImageContext(self.bounds.size);
[self.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// hack, helps w/ our colors when blurring
NSData *imageData = UIImageJPEGRepresentation(image, 1); // convert to jpeg
image = [UIImage imageWithData:imageData];
return image;
}
@end
#pragma mark - UIImage + Blur
@implementation UIImage (Blur)
-(UIImage *)boxblurImageWithBlur:(CGFloat)blur {
if (blur < 0.f || blur > 1.f) {
blur = 0.5f;
}
int boxSize = (int)(blur * 40);
boxSize = boxSize - (boxSize % 2) + 1;
CGImageRef img = self.CGImage;
vImage_Buffer inBuffer, outBuffer;
vImage_Error error;
void *pixelBuffer;
//create vImage_Buffer with data from CGImageRef
CGDataProviderRef inProvider = CGImageGetDataProvider(img);
CFDataRef inBitmapData = CGDataProviderCopyData(inProvider);
inBuffer.width = CGImageGetWidth(img);
inBuffer.height = CGImageGetHeight(img);
inBuffer.rowBytes = CGImageGetBytesPerRow(img);
inBuffer.data = (void*)CFDataGetBytePtr(inBitmapData);
//create vImage_Buffer for output
pixelBuffer = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
if(pixelBuffer == NULL)
NSLog(@"No pixelbuffer");
outBuffer.data = pixelBuffer;
outBuffer.width = CGImageGetWidth(img);
outBuffer.height = CGImageGetHeight(img);
outBuffer.rowBytes = CGImageGetBytesPerRow(img);
// Create a third buffer for intermediate processing
void *pixelBuffer2 = malloc(CGImageGetBytesPerRow(img) * CGImageGetHeight(img));
vImage_Buffer outBuffer2;
outBuffer2.data = pixelBuffer2;
outBuffer2.width = CGImageGetWidth(img);
outBuffer2.height = CGImageGetHeight(img);
outBuffer2.rowBytes = CGImageGetBytesPerRow(img);
//perform convolution
error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer2, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
error = vImageBoxConvolve_ARGB8888(&outBuffer2, &inBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
error = vImageBoxConvolve_ARGB8888(&inBuffer, &outBuffer, NULL, 0, 0, boxSize, boxSize, NULL, kvImageEdgeExtend);
if (error) {
NSLog(@"error from convolution %ld", error);
}
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(outBuffer.data,
outBuffer.width,
outBuffer.height,
8,
outBuffer.rowBytes,
colorSpace,
kCGImageAlphaNoneSkipLast);
CGImageRef imageRef = CGBitmapContextCreateImage (ctx);
UIImage *returnImage = [UIImage imageWithCGImage:imageRef];
//clean up
CGContextRelease(ctx);
CGColorSpaceRelease(colorSpace);
free(pixelBuffer);
CFRelease(inBitmapData);
CGImageRelease(imageRef);
return returnImage;
}
@end