ios (Quartz 2D绘图)各种绘图方式及相机的使用
一:
具体使用的细节,本人也是参考http://blog.163.com/wkyuyang_001/blog/static/10802122820133190545227/
下面介绍具体使用Quartz 2D绘图实现画图板功能
.m文件中,dog的实现如连接中所示一样的
<pre name="code" class="objc">#import "drawTestView.h" #import "Dog.h" @implementation drawTestView @synthesize dogs,tempdogs; - (NSMutableArray*)dogs{ if (dogs == nil) { dogs = [NSMutableArray array]; } return dogs; } - (NSMutableArray*)tempdogs{ if (tempdogs == nil) { tempdogs = [NSMutableArray array]; } return tempdogs; } - (void)drawRect:(CGRect)rect{ CGContextRef ctx = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(ctx,[[UIColor greenColor] CGColor]); //画图板功能 [dogs enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){ NSMutableArray*arr = [dogs objectAtIndex:idx]; for (int i = 0; i<[arr count]; i++) { Dog*dog = (Dog*)[arr objectAtIndex:i]; if (i == 0) { CGContextMoveToPoint(ctx, dog.x, dog.y); }else{ CGContextAddLineToPoint(ctx, dog.x, dog.y); } } }]; [[UIColor blackColor]setStroke]; CGContextDrawPath(ctx, kCGPathStroke); } - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch*touch = [touches anyObject]; CGPoint location = [touch locationInView:self]; Dog*dog = [[Dog alloc]init]; dog.x = location.x; dog.y = location.y; [self.tempdogs addObject:dog]; [self.dogs addObject:self.tempdogs]; [self setNeedsDisplay];//调用draw方法 } - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch*touch = [touches anyObject]; CGPoint location = [touch locationInView:self]; Dog*dog = [[Dog alloc]init]; dog.x = location.x; dog.y = location.y; [self.tempdogs addObject:dog]; [self.dogs addObject:[self.tempdogs copy]]; [self setNeedsDisplay];//调用draw方法 } - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ [tempdogs removeAllObjects]; }
二:相机的使用
要在使用的viewcontroller中实现
<UINavigationControllerDelegate,UIImagePickerControllerDelegate>//如果只写一个UIImagePickerControllerDelegate,会出现警告,甚至不会调用代理方法
imagePicker = [[UIImagePickerController alloc]init]; imagePicker.delegate = self;
进入相机的方法
- (IBAction)goImagePicker:(id)sender { if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; self.imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;//调用前置像头 }else{ self.imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; } self.imagePicker.allowsEditing = YES; [self presentViewController:self.imagePicker animated:YES completion:nil]; }
实现两个代理方法:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ NSLog(@"select"); UIImage * image = [info objectForKey:UIImagePickerControllerEditedImage]; _imageSelect.frame = CGRectMake(_imageSelect.frame.origin.x, _imageSelect.frame.origin.y, _imageSelect.frame.size.width, _imageSelect.frame.size.width*image.size.height/image.size.width);//为了不让选出来的图片变形 _imageSelect.image = image; [self.imagePicker dismissViewControllerAnimated:YES completion:nil]; } - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ [self.imagePicker dismissViewControllerAnimated:YES completion:nil]; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。