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];
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值