1

私のアプリケーションではUIImagePickerController、写真を撮るために a を使用しましたが、写真を撮った後UIImageView、指で線を描きたいので、 を使用して描画できません。

この目的のために、draw rect メソッドで次のコードを記述しました。

- (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);

}

そして触れて移動しました:

- (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];指を動かすたびに呼び出すのは正しいですか、それとももっと良い方法がありますか?

4

1 に答える 1

1

私はこれを行いましたが、別の問題で立ち往生しています...ここにあなたの解決策があります...

- (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です。

于 2009-09-17T13:00:18.327 に答える