我正在制作一个绘图应用程序,我正在使用故事板,所以我可以用它来实现一个游戏。 drawingViewController的UIimageViewer只占用屏幕的大约3/4,因为另外1/4用于保持按钮和句子。但是,每次绘制线条时,图像都会沿y轴拉伸。我的问题是,我能做些什么来让它不再延伸。或者如果我让UIimageViewer占据整个屏幕并将按钮放在顶部会更好吗?我还需要在UIimageview的顶部添加标签和文本框?我是iOS 6编程的新手,所以任何帮助都将非常感激。
#import "DrawViewController.h"
@interface DrawViewController ()
@end
@implementation DrawViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
red = 0.0/255.0;
green = 0.0/255.0;
blue = 0.0/255.0;
brush = 10.0;
opacity = 1.0;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)pencilPressed:(id)sender {
UIButton * PressedButton = (UIButton*)sender;
switch(PressedButton.tag)
{
case 0:
red = 0.0/255.0;
green = 0.0/255.0;
blue = 0.0/255.0;
break;
case 1:
red = 105.0/255.0;
green = 105.0/255.0;
blue = 105.0/255.0;
break;
case 2:
red = 255.0/255.0;
green = 0.0/255.0;
blue = 0.0/255.0;
break;
case 3:
red = 0.0/255.0;
green = 0.0/255.0;
blue = 255.0/255.0;
break;
case 4:
red = 102.0/255.0;
green = 204.0/255.0;
blue = 0.0/255.0;
break;
case 5:
red = 102.0/255.0;
green = 255.0/255.0;
blue = 0.0/255.0;
break;
case 6:
red = 51.0/255.0;
green = 204.0/255.0;
blue = 255.0/255.0;
break;
case 7:
red = 160.0/255.0;
green = 82.0/255.0;
blue = 45.0/255.0;
break;
case 8:
red = 255.0/255.0;
green = 102.0/255.0;
blue = 0.0/255.0;
break;
case 9:
red = 255.0/255.0;
green = 255.0/255.0;
blue = 0.0/255.0;
break;
}
}
- (IBAction)eraserPressed:(id)sender {
red = 255.0/255.0;
green = 255.0/255.0;
blue = 255.0/255.0;
opacity = 1.0;
}
- (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 :(得分:0)
请确保Imageview&该imageview上的图像大小相同。