将 UIImage 绘制到 CurrentContext 中

发布于 2024-07-25 13:11:26 字数 1249 浏览 1 评论 0原文

在我的应用程序中,我使用 UIImagePickerController 来拍照,拍完照片后,我无法使用 UIImageView 绘制它,因为我想绘制一些线条用我的手指在上面。

为此,我在绘制矩形方法中编写了此代码:

- (void)drawRect:(CGRect)rect { 
    [self.myimage drawInRect: rect];

    //Disegno rettangolo del tag
    CGContextRef myContext = UIGraphicsGetCurrentContext();

    CGContextSetRGBStrokeColor(myContext, 0.5, 0.5, 0.5, 1.0);

    CGContextSetLineWidth(myContext, 3.0f);

    CGContextAddPath(myContext, pathTouches);

    CGContextStrokePath(myContext);

}

并在touchesMoved中编写了:

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

    UITouch *touch = [touches anyObject];
    CGPoint locationInView = [touch locationInView:self];

    // Draw a connected sequence of line segments
    CGPoint addLines[] =
    {
        //....
            //some lines
    };

    CGMutablePathRef newPath = CGPathCreateMutable();
    CGPathRelease(pathTouches);
    pathTouches = CGPathCreateMutableCopy(newPath);
    CGPathRelease(newPath);

    CGPathAddLines(pathTouches, NULL, addLines, sizeof(addLines)/sizeof(addLines[0]));

    [self setNeedsDisplay];

}

每次移动手指时调用[self setNeedsDisplay];是否正确,或者有更好的方法来做到这一点?

In my application I've used a UIImagePickerController to take a photo, and after I took the photo, I can't draw it using UIImageView because I want to draw some lines on it with my finger.

To this end I have written in draw rect methods this code:

- (void)drawRect:(CGRect)rect { 
    [self.myimage drawInRect: rect];

    //Disegno rettangolo del tag
    CGContextRef myContext = UIGraphicsGetCurrentContext();

    CGContextSetRGBStrokeColor(myContext, 0.5, 0.5, 0.5, 1.0);

    CGContextSetLineWidth(myContext, 3.0f);

    CGContextAddPath(myContext, pathTouches);

    CGContextStrokePath(myContext);

}

and in touchesMoved:

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

    UITouch *touch = [touches anyObject];
    CGPoint locationInView = [touch locationInView:self];

    // Draw a connected sequence of line segments
    CGPoint addLines[] =
    {
        //....
            //some lines
    };

    CGMutablePathRef newPath = CGPathCreateMutable();
    CGPathRelease(pathTouches);
    pathTouches = CGPathCreateMutableCopy(newPath);
    CGPathRelease(newPath);

    CGPathAddLines(pathTouches, NULL, addLines, sizeof(addLines)/sizeof(addLines[0]));

    [self setNeedsDisplay];

}

Is it correct to call [self setNeedsDisplay]; every time I move my finger, or there is a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经暖 2024-08-01 13:11:27

我已经做到了这一点,但遇到了另一个问题...这是您的解决方案...

- (void)imagePickerController:(UIImagePickerController *)picker 
    didFinishPickingImage:(UIImage *)image
              editingInfo:(NSDictionary *)editingInfo
{
// Dismiss the image selection, hide the picker and
//show the image view with the picked image
[picker dismissModalViewControllerAnimated:YES];
imagePickerController.view.hidden = YES;
[imagePickerController release];
imageView.image = image;
imageView.hidden = NO;
[imageView setFrame:CGRectMake(0, 20, 320, 396)];
[window bringSubviewToFront:imageView];
}

然后

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == EditImageView)
{
    lastPoint = currentPoint;
    lastPoint = [touch locationInView:self.view];

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

//sets the style for the endpoints of lines drawn in a graphics context
ctx = UIGraphicsGetCurrentContext();
CGContextSetLineCap(ctx, kCGLineCapRound);
//sets the line width for a graphic context
CGContextSetLineWidth(ctx,10.0);
//set the line colour
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
//creates a new empty path in a graphics context
CGContextBeginPath(ctx);
    CGContextSetAllowsAntialiasing(ctx,TRUE);
//begin a new path at the point you specify
CGContextMoveToPoint(ctx, currentPoint.x, currentPoint.y);
//Appends a straight line segment from the current point to the provided point 
CGContextAddLineToPoint(ctx, lastPoint.x,lastPoint.y);
//paints a line along the current path
CGContextStrokePath(ctx);
EditImageView.image = UIGraphicsGetImageFromCurrentImageContext();
    [EditImageView setNeedsDisplay];

    CGContextSaveGState(ctx);
    UIGraphicsEndImageContext();
     currentPoint = lastPoint;
}   
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject]; 
if ([touch view] == EditImageView)
{
    currentPoint=[touch locationInView:self.view];
}
}

currentPoint 和 lastPoint 是 CGPoint 在头文件中声明的位置。

i have done this but stuck at another problem...here is your solution...

- (void)imagePickerController:(UIImagePickerController *)picker 
    didFinishPickingImage:(UIImage *)image
              editingInfo:(NSDictionary *)editingInfo
{
// Dismiss the image selection, hide the picker and
//show the image view with the picked image
[picker dismissModalViewControllerAnimated:YES];
imagePickerController.view.hidden = YES;
[imagePickerController release];
imageView.image = image;
imageView.hidden = NO;
[imageView setFrame:CGRectMake(0, 20, 320, 396)];
[window bringSubviewToFront:imageView];
}

then

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
if ([touch view] == EditImageView)
{
    lastPoint = currentPoint;
    lastPoint = [touch locationInView:self.view];

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

//sets the style for the endpoints of lines drawn in a graphics context
ctx = UIGraphicsGetCurrentContext();
CGContextSetLineCap(ctx, kCGLineCapRound);
//sets the line width for a graphic context
CGContextSetLineWidth(ctx,10.0);
//set the line colour
CGContextSetRGBStrokeColor(ctx, 1.0, 0.0, 0.0, 1.0);
//creates a new empty path in a graphics context
CGContextBeginPath(ctx);
    CGContextSetAllowsAntialiasing(ctx,TRUE);
//begin a new path at the point you specify
CGContextMoveToPoint(ctx, currentPoint.x, currentPoint.y);
//Appends a straight line segment from the current point to the provided point 
CGContextAddLineToPoint(ctx, lastPoint.x,lastPoint.y);
//paints a line along the current path
CGContextStrokePath(ctx);
EditImageView.image = UIGraphicsGetImageFromCurrentImageContext();
    [EditImageView setNeedsDisplay];

    CGContextSaveGState(ctx);
    UIGraphicsEndImageContext();
     currentPoint = lastPoint;
}   
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject]; 
if ([touch view] == EditImageView)
{
    currentPoint=[touch locationInView:self.view];
}
}

where currentPoint and lastPoint are CGPoint Declared in Header File.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文