您的位置:首页 > 移动开发 > IOS开发

ios (Quartz 2D绘图)各种绘图方式及相机的使用

2015-01-07 18:35 363 查看
一:

具体使用的细节,本人也是参考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];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息