-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathQRImageView.m
More file actions
64 lines (50 loc) · 1.87 KB
/
QRImageView.m
File metadata and controls
64 lines (50 loc) · 1.87 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
//
// QRImageView.m
// QRCodeEncoderObjectiveCAtGithub
//
// Created by Shiva Huang on 13/6/18.
//
//
#import "QRImageView.h"
@implementation QRImageView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.borderWidth = 4;
}
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [self.backgroundColor CGColor]);
CGContextFillRect(context, rect);
if(self.dataMatrix == nil)
return;
const int dataDimension = self.dataMatrix.dimension;
const CGFloat imageDimension = MIN(self.frame.size.width, self.frame.size.height);
const CGFloat pixelPerDot = imageDimension / (dataDimension + self.borderWidth * 2);
CGFloat yMargin = (self.frame.size.height - imageDimension) / 2.0;
CGFloat xMargin = (self.frame.size.width - imageDimension) / 2.0;
CGFloat yOffset = 0.0;
// Drawing code
CGContextSetFillColorWithColor(context, [[UIColor whiteColor] CGColor]);
CGContextFillRect(context, CGRectMake(xMargin, yMargin, imageDimension, imageDimension));
CGContextSetFillColorWithColor(context, [[UIColor blackColor] CGColor]);
yMargin += self.borderWidth * pixelPerDot;
xMargin += self.borderWidth * pixelPerDot;
for(int my=0; my<dataDimension; my++) {
yOffset = yMargin + my * pixelPerDot;
for(int mx=0; mx<dataDimension; mx++) {
if([self.dataMatrix valueAt:mx y:my]) {
CGContextAddRect(context, CGRectMake(xMargin + mx * pixelPerDot, yOffset, pixelPerDot, pixelPerDot));
}
}
}
CGContextFillPath(context);
}
@end