ios (Quartz 2D繪圖)各種繪圖方式及相機的使用

來源:互聯網
上載者:User

標籤:quartz   圖片   2d   uiimagepickercontrol   畫圖板   


一:

具體使用的細節,本人也是參考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];}


ios (Quartz 2D繪圖)各種繪圖方式及相機的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.