守望远方

热爱生活 热爱工作 守望远方
IOS学习笔记(二)取图片

实现动态选取矩形区域内image的功能:

主要思路:因为要随着用户的矩形区域选择,动态显示图片,还要把选择的矩形区域用虚线标识出来,所以要用到重绘(drawRect),then,就需要定义一个UIView的子视图,重写drawRect方法。显示选择区域的部分,用touchBegan,touchMove,touchEnd方法实现。

MyImageView.h

1 @interface MyImageView : UIView
2 @property CGPoint startPoint;
3 @property CGRect rectangle;
4 @property(strong,nonatomic)NSMutableArray *lineArray;
5 @property(strong,nonatomic)UIImage *image;
6 //-(void)setBackgroundImage;
7 @end

MyImageView.m

MyImageView.m
 1 @implementation MyImageView
 2 @synthesize startPoint = _startPoint;
 3 @synthesize rectangle = _rectangle;
 4 @synthesize lineArray = _lineArray;
 5 @synthesize image = _image;
 6 - (id)initWithFrame:(CGRect)frame
 7 {
 8     self = [super initWithFrame:frame];
 9     if (self) {
10         // Initialization code
11 //        UIImage *image = [UIImage imageNamed:@"iphone.jpg"];
12 //        //CGPoint drawPoint = CGPointMake(0, 0);
13 //        [image drawInRect:self.frame];
14     }
15     return self;
16 }
17 
18 // Only override drawRect: if you perform custom drawing.
19 // An empty implementation adversely affects performance during animation.
20 - (void)drawRect:(CGRect)rect
21 {
22     //UIGraphicsBeginImageContext
23     // Drawing code
24     CGContextRef context = UIGraphicsGetCurrentContext();
25     
26     CGFloat dashPhase = 5.0;//dash:破折号 phase:相位
27     CGFloat dashPattern[10] = {10.0,10.0};//
28     size_t dashCount = 2;//dashPattern中的数的个数
29     CGContextSetLineDash(context, dashPhase, dashPattern, dashCount);
30     //CGPoint drawPoint = CGPointMake(0, 0);
31     CGContextAddRect(context, self.rectangle);
32     CGContextSetRGBStrokeColor(context,0,0, 0, 0.5);
33     CGContextSetLineWidth(context, 0.5);
34     CGContextStrokePath(context);
35     
36     
37     CGPoint drawPoint = self.frame.origin;
38     [self.image drawAtPoint:drawPoint];
39     CGContextStrokePath(context);
40     
41 }
42 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
43 {
44     NSMutableArray *pointArray = [[NSMutableArray alloc]init];
45     UITouch *touch = [touches anyObject];
46     self.startPoint = [touch locationInView:self];
47     NSValue *pointValue = [NSValue valueWithCGPoint:self.startPoint];
48     [pointArray addObject:pointValue];
49     [self.lineArray addObject:pointArray];
50     [pointArray release];
51     
52 }
53 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
54 {
55 //    UIGraphicsBeginImageContext(self.rectangle.size);
56 //    UIGraphicsGetCurrentContext();
57 //    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
58 //    self.image = image;
59     UIImage *image = [UIImage imageNamed:@"iphone.jpg"];
60     CGImageRef sourceImageRef = [image CGImage];  
61     CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, self.rectangle);  
62     UIImage *newImage = [UIImage imageWithCGImage:newImageRef];  
63     self.image = newImage;
64     [self setNeedsDisplay];
65 }
66 -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
67 {
68     UITouch *touch = [touches anyObject];
69     CGPoint endPoint = [touch locationInView:self];
70     NSValue *pointValue = [NSValue valueWithCGPoint:endPoint];
71     NSMutableArray *pointArray = [self.lineArray lastObject];
72     [pointArray addObject:pointValue];
73     float dx =  endPoint.x - self.startPoint.x;
74     float dy = endPoint.y - self.startPoint.y;
75     if(dx > 0 && dy > 0)
76     {
77         self.rectangle = CGRectMake(self.startPoint.x, self.startPoint.y, dx, dy);
78         [self setNeedsDisplay];
79     }
80     
81 }
82 @end

posted on 2012-09-17 17:43  守望远方  阅读(414)  评论(0编辑  收藏  举报