0

私は画像編集アプリに取り組んでいます。現在、ユーザーがライブラリから写真を選択したり、カメラで写真を撮ったりできるようにアプリを作成しています。また、ユーザーが選択できる他の画像を含む別のビュー (ピッカー ビュー) もあります。画像の 1 つを選択すると、アプリはユーザーをメインの写真に戻します。

ユーザーが画面上のどこにでも触れて、選択した画像を追加できるようにしたい。

これにアプローチする最良の方法は何ですか?

touchesBegan? touchesMoved? UITapGestureRecognizer?

誰かがサンプルコードを知っているか、これにアプローチする方法の一般的なアイデアを教えてくれれば、それは素晴らしいことです!

編集

これで、座標が表示され、UIImage がピッカーから選択した画像を取得していることがわかります。しかし、タップしても画像が画面に表示されません。誰かが私のコードのトラブルシューティングを手伝ってくれませんか:

-(void)drawRect:(CGRect)rect
{    
    CGRect currentRect = CGRectMake(touchPoint.x, touchPoint.y, 30.0, 30.0);

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextFillRect(context, currentRect);
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [touches anyObject];
    touchPoint = [touch locationInView:imageView];

    NSLog(@"%f", touchPoint.x);
    NSLog(@"%f", touchPoint.y);

    if (touchPoint.x > -1 && touchPoint.y > -1) 
    {
        stampedImage = _imagePicker.selectedImage;   

        //[stampedImage drawAtPoint:touchPoint];

        [_stampedImageView setFrame:CGRectMake(touchPoint.x, touchPoint.y, 30.0, 30.0)];

        [_stampedImageView setImage:stampedImage];

        [imageView addSubview:_stampedImageView];

        NSLog(@"Stamped Image = %@", stampedImage);

        //[self.view setNeedsDisplay];
    }
}

私の NSLogs の例については、次のように表示されます。

162.500000
236.000000
Stamped Image = <UIImage: 0xe68a7d0>

ありがとう!

4

1 に答える 1

0

ViewController で、「-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event」メソッドを使用して、タッチが発生した場所の X 座標と Y 座標を取得します。タッチの x と y を取得する方法を示すサンプル コードを次に示します。

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    /* Detect touch anywhere */
    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];

    NSLog(@"%f", touchPoint.x);  // The x coordinate of the touch
    NSLog(@"%f", touchPoint.y);  // The y coordinate of the touch
}

この x と y のデータを取得したら、ユーザーが選択した画像または内蔵カメラで撮影した画像をそれらの座標に表示するように設定できます。


編集:

問題は、UIImage ビューの作成方法にあると思います。これの代わりに:

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [touches anyObject];
    touchPoint = [touch locationInView:imageView];

    CGRect myImageRect = CGRectMake(touchPoint.x, touchPoint.y, 20.0f, 20.0f);
    UIImageView * myImage = [[UIImageView alloc] initWithFrame:myImageRect];
    [myImage setImage:_stampedImageView.image];
    myImage.opaque = YES;
    [imageView addSubview:myImage];
    [myImage release];
}

これを試して:

 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [touches anyObject];
    touchPoint = [touch locationInView:imageView];

    myImage = [[UIImageView alloc] initWithImage:_stampedImageView.image];
    [imageView addSubview:myImage];
    [myImage release];
}

これが機能しない場合は、「_stampedImageView.image == nil」かどうかを確認してください。これが当てはまる場合、UIImage が正しく作成されていない可能性があります。

于 2012-07-17T03:27:08.033 に答える