一:
具體使用的細節,本人也是參考www.2cto.com
下面介紹具體使用Quartz 2D繪圖實現畫圖板功能
.m文件中,dog的實現如連接中所示一樣的
#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中實現
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]; }