2015-03-02 51 views
0

下午好,繪圖時保持相同的圖像比例

我想創建一個繪圖應用程序,我可以在圖像上寫字。問題是當我試圖這樣做時,我有一些Draw功能的問題。

當我觸摸屏幕開始繪製一切都很好時,但是當我完成繪製時(通過不觸摸屏幕),圖像從正常圖像變爲調整大小。

我如何能保持當我繪製同一圖像比例是多少?

這裏是它發生:

那個時候我沒有觸摸屏幕:

http://www.idermo.es/IMG_0275.PNG

,這時候我的感動和完成了我的畫:

http://www.idermo.es/IMG_0276.PNG

這是我的全碼:

// DrawViewController.m 


#import "DrawViewController.h" 


@interface DrawViewController() 

@end 

@implementation DrawViewController 

@synthesize mainImage; 

@synthesize tempDrawImage; 



- (void)viewDidLoad 

{ 

    red = 0.0/255.0; 

    green = 0.0/255.0; 

    blue = 0.0/255.0; 

    brush = 5.0; 

    opacity = 1.0; 

    self.mainImage.image = _image; 



    [super viewDidLoad]; 

} 



- (void)viewDidUnload 

{ 

    [self setMainImage:nil]; 

    [self setTempDrawImage:nil]; 

    [super viewDidUnload]; 

    // Release any retained subviews of the main view. 

} 



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

{ 

    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 

} 



- (IBAction)pencilPressed:(id)sender { 



    red = 0.0/255.0; 

    green = 0.0/255.0; 

    blue = 0.0/255.0; 

} 



- (IBAction)reset:(id)sender { 



    self.mainImage.image = nil; 



} 



- (IBAction)save:(id)sender { 



    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"" 

                  delegate:self 

                cancelButtonTitle:nil 

               destructiveButtonTitle:nil 

                otherButtonTitles:@"Save to Camera Roll", @"Tweet it!", @"Cancel", nil]; 

    [actionSheet showInView:self.view]; 

} 



- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 

{ 

    if (buttonIndex == 1) 

    { 



    } else if(buttonIndex == 0) { 



     UIGraphicsBeginImageContextWithOptions(self.mainImage.bounds.size, NO, 0.0); 

     [self.mainImage.image drawInRect:CGRectMake(0, 0, self.mainImage.frame.size.width, self.mainImage.frame.size.height)]; 

     UIImage *SaveImage = UIGraphicsGetImageFromCurrentImageContext(); 

     UIGraphicsEndImageContext(); 

     UIImageWriteToSavedPhotosAlbum(SaveImage, self,@selector(image:didFinishSavingWithError:contextInfo:), nil); 

    } 

} 



- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo 

{ 

    // Was there an error? 

    if (error != NULL) 

    { 

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Image could not be saved.Please try again" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Close", nil]; 

     [alert show]; 

    } else { 

     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Success" message:@"Image was successfully saved in photoalbum" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Close", nil]; 

     [alert show]; 

    } 

} 



- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 



    mouseSwiped = NO; 

    UITouch *touch = [touches anyObject]; 

    lastPoint = [touch locationInView:self.view]; 

} 



- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 



    mouseSwiped = YES; 

    UITouch *touch = [touches anyObject]; 

    CGPoint currentPoint = [touch locationInView:self.view]; 



    UIGraphicsBeginImageContext(self.view.frame.size); 

    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 

    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y); 

    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 

    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush); 

    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, 1.0); 

    CGContextSetBlendMode(UIGraphicsGetCurrentContext(),kCGBlendModeNormal); 



    CGContextStrokePath(UIGraphicsGetCurrentContext()); 

    self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 

    [self.tempDrawImage setAlpha:opacity]; 

    UIGraphicsEndImageContext(); 



    lastPoint = currentPoint; 

} 



- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 



    if(!mouseSwiped) { 

     UIGraphicsBeginImageContext(self.view.frame.size); 

     [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; 

     CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); 

     CGContextSetLineWidth(UIGraphicsGetCurrentContext(), brush); 

     CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), red, green, blue, opacity); 

     CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 

     CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y); 

     CGContextStrokePath(UIGraphicsGetCurrentContext()); 

     CGContextFlush(UIGraphicsGetCurrentContext()); 

     self.tempDrawImage.image = UIGraphicsGetImageFromCurrentImageContext(); 

     UIGraphicsEndImageContext(); 

    } 



    UIGraphicsBeginImageContext(self.mainImage.frame.size); 

    [self.mainImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:1.0]; 

    [self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity]; 

    self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext(); 

    self.tempDrawImage.image = nil; 

    UIGraphicsEndImageContext(); 

} 

@end 

我如何能保持當我繪製同一圖像比例是多少?

在此先感謝。

+0

如果'self.mainImage'和'self.view'是不一樣的尺寸,告訴你的圖像在不同矩形尺寸的上下文中繪製可能會產生不良影響。 – 2015-03-02 14:21:51

+0

我已經在xcode中將我的mainImage設置爲「重畫」,因爲我想用正確的比例顯示「真實」圖像,而不是縮放比例。 – BBangF1 2015-03-02 14:24:56

+0

但是'self.mainImage'和'self.view'是相同的** size **嗎? – 2015-03-02 14:32:54

回答

0

我認爲這個問題是在你接觸的最後部分結束代碼。我覺得你的UIImage(mainImage.image)小於該UIImageView的(mainImage)

嘗試下面的代碼:

UIGraphicsBeginImageContext(self.mainImage.frame.size); 

[self.mainImage.image drawInRect:CGRectMake((self.mainImage.frame.size.width - self.mainImage.image.size.width)/2, (self.mainImage.frame.size.height - self.mainImage.image.size.height)/2, self.mainImage.image.size.width, mainImage.image.size.height) blendMode:kCGBlendModeNormal alpha:1.0];//mainImage.image is smaller than mainImage's frame. So draw it at appropiate position and size, so no scaling is caused. 

[self.tempDrawImage.image drawInRect:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) blendMode:kCGBlendModeNormal alpha:opacity]; 

self.mainImage.image = UIGraphicsGetImageFromCurrentImageContext(); 

self.tempDrawImage.image = nil; 

UIGraphicsEndImageContext(); 
相關問題