forked from sprang/Inkpad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWDImageView.m
More file actions
59 lines (46 loc) · 1.41 KB
/
WDImageView.m
File metadata and controls
59 lines (46 loc) · 1.41 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
//
// WDImageView.m
// Inkpad
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
// Copyright (c) 2013 Steve Sprang
//
#import "WDImageView.h"
#import "WDUtilities.h"
@implementation WDImageView
@synthesize image = image_;
@synthesize maximumDimension = maximumDimension_;
- (id)initWithImage:(UIImage *)image maxDimension:(float)maxDimension
{
self = [super initWithFrame:WDRectFromSize(image.size)];
if (!self) {
return nil;
}
self.maximumDimension = maxDimension;
self.image = image;
return self;
}
- (void) setImage:(UIImage *)anImage
{
image_ = anImage;
// set the image frame so that it appears correctly on retina and non-retina displays
CGRect frame = CGRectMake(0, 0, anImage.size.width, anImage.size.height);
float aspectRatio = frame.size.width / frame.size.height;
if (aspectRatio > 1.0f) {
frame.size.width = self.maximumDimension;
frame.size.height = (1.0f / aspectRatio) * self.maximumDimension;
} else {
frame.size.height = self.maximumDimension;
frame.size.width = aspectRatio * self.maximumDimension;
}
self.frame = frame;
[self setNeedsDisplay];
}
- (void) drawRect:(CGRect)rect
{
[self.image drawInRect:self.bounds];
}
@end