forked from dmorrow/ObjectiveC-Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUTImageView.m
112 lines (90 loc) · 2.76 KB
/
UTImageView.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
//
// UTImageView.m
// RingFinder
//
// Created by Daniel Morrow on 4/3/10.
// Copyright 2010 Tiffany & Co.. All rights reserved.
//
#import "UTImageView.h"
@implementation UTImageView
@synthesize image;
@synthesize quality;
@synthesize trans;
@synthesize previousScale;
-(UTImageView *) initWithImage:(UIImage *)img quality:(CGInterpolationQuality)qual
{
//CGRect rect = CGRectMake(0, 0, img.size.width, img.size.height);
//NSLog(@"made utimageview");
if (self = [super initWithImage:img])
{
[self setUserInteractionEnabled:YES];
[self setMultipleTouchEnabled:YES];
//self.image = img;
self.quality = qual;
self.trans = CGAffineTransformIdentity;
self.previousScale = 1.0f;
//self.contentMode = UIViewContentModeRedraw;
}
return self;
}
-(void) drawRect:(CGRect)rect
{
if (self.image)
{
//NSLog(@"redraw");
int width = rect.size.width;
int height = rect.size.height;
DLog(@"width:%i, height:%i", width, height);
CGRect imageRect;
if (image.imageOrientation == UIImageOrientationUp || image.imageOrientation == UIImageOrientationDown)
{
imageRect = CGRectMake(0, 0, width, height);
}
else
{
imageRect = CGRectMake(0, 0, height, width);
}
//rect = CGRectApplyAffineTransform(rect, trans);
//CGAffineTransform newTrans = CGAffineTransformScale(trans, 1.0f/previousScale, 1.0f/previousScale);
//self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, imageRect.size.width, imageRect.size.height);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, 0, height);
CGContextScaleCTM(context, 1.0, -1.0);
switch (image.imageOrientation)
{
case UIImageOrientationLeft:
CGContextRotateCTM(context, M_PI/2);
CGContextTranslateCTM(context, 0, -width);
break;
case UIImageOrientationRight:
CGContextRotateCTM(context, -M_PI/2);
CGContextTranslateCTM(context, -height, 0);
break;
case UIImageOrientationDown:
CGContextTranslateCTM(context, width, height);
CGContextRotateCTM(context, M_PI);
break;
default:
break;
}
CGContextSetInterpolationQuality(context, self.quality);
CGContextDrawImage(context, imageRect, self.image.CGImage);
//CGContextRelease(context);
}
}
-(void) redrawAtScale:(float)newScale
{
CGAffineTransform newTransform = CGAffineTransformScale(self.transform, 1.0f/newScale, 1.0f/newScale);
[super setTransform:CGAffineTransformIdentity];
self.frame = CGRectApplyAffineTransform(self.frame, newTransform);
[self setNeedsDisplay];
}
-(void) setTransform:(CGAffineTransform)newValue
{
[super setTransform:CGAffineTransformScale(newValue, 1.0f/previousScale, 1.0f/previousScale)];
}
-(void) setTransformWithoutScaling:(CGAffineTransform)newTransform
{
[super setTransform:newTransform];
}
@end