我在View Controller中有View Controller我已经放置了一个UIImageView我试图以这种方式在UIImageView中显示图像。
- (void)viewDidLoad {
[super viewDidLoad];
[self.drawImageView setImage:editableImage];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(drawingViewDidPan:)];
panGesture.maximumNumberOfTouches = 1;
self.drawImageView.userInteractionEnabled = YES;
[self.drawImageView addGestureRecognizer:panGesture];
}
其中drawImageView是我的UIImageView,editableImage是我试图绘制的UIImage。
我正在实施绘图部分如下
- (void)drawingViewDidPan:(UIPanGestureRecognizer*)sender
{
CGPoint currentDraggingPosition = [sender locationInView:drawImageView];
if(sender.state == UIGestureRecognizerStateBegan){
prevDraggingPosition = currentDraggingPosition;
}
if(sender.state != UIGestureRecognizerStateEnded){
[self drawLine:prevDraggingPosition to:currentDraggingPosition];
}
prevDraggingPosition = currentDraggingPosition;
}
-(void)drawLine:(CGPoint)from to:(CGPoint)to
{
CGSize size = drawImageView.frame.size;
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[drawImageView.image drawAtPoint:CGPointZero];
CGFloat strokeWidth = 5.0;
UIColor *strokeColor = [UIColor redColor];
CGContextSetLineWidth(context, strokeWidth);
CGContextSetStrokeColorWithColor(context, strokeColor.CGColor);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextMoveToPoint(context, from.x, from.y);
CGContextAddLineToPoint(context, to.x, to.y);
CGContextStrokePath(context);
drawImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
问题是,一旦我用手指画出UIImage调整大小并变得小休息一切都很好
我该如何解决这个问题?
我的editableImage来自另一个viewcontroller,它有一个名为draw
的按钮-(IBAction)drawOnOmage:(id)sender {
[self performSegueWithIdentifier:@"MySegue1" sender:self];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
ThirdViewController *destinationController = (ThirdViewController *)segue.destinationViewController;
destinationController->editableImage = self.editImage.image;
}
答案 0 :(得分:0)
我刚刚将您的代码复制到一个新项目中,它似乎对我有用。我唯一注意到的是你没有对@property
使用prevDraggingPosition
声明 - 也许这就是问题所在?你能告诉我们你如何申报prevDraggingPosition
吗?
看看我的示例(对我有用,我只添加了self.
):
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic, strong) IBOutlet UIImageView *drawImageView;
@property (nonatomic, assign) CGPoint prevDraggingPosition;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(drawingViewDidPan:)];
panGesture.maximumNumberOfTouches = 1;
self.drawImageView.userInteractionEnabled = YES;
[self.drawImageView addGestureRecognizer:panGesture];
}
- (void)drawingViewDidPan:(UIPanGestureRecognizer*)sender
{
CGPoint currentDraggingPosition = [sender locationInView:self.drawImageView];
if(sender.state == UIGestureRecognizerStateBegan){
self.prevDraggingPosition = currentDraggingPosition;
}
if(sender.state != UIGestureRecognizerStateEnded){
[self drawLine:self.prevDraggingPosition to:currentDraggingPosition];
}
self.prevDraggingPosition = currentDraggingPosition;
}
-(void)drawLine:(CGPoint)from to:(CGPoint)to
{
CGSize size = self.drawImageView.frame.size;
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
CGContextRef context = UIGraphicsGetCurrentContext();
[self.drawImageView.image drawAtPoint:CGPointZero];
CGFloat strokeWidth = 5.0;
UIColor *strokeColor = [UIColor redColor];
CGContextSetLineWidth(context, strokeWidth);
CGContextSetStrokeColorWithColor(context, strokeColor.CGColor);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextMoveToPoint(context, from.x, from.y);
CGContextAddLineToPoint(context, to.x, to.y);
CGContextStrokePath(context);
self.drawImageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
@end