2011-01-06 9 views
4

Ich möchte ein Rechteck auf ein Bild zeichnen Wie geht das?uiImage Ansicht und Rechteck

+0

meinst du etwas über dem Bild zu zeichnen? – shannoga

+0

ja ich möchte auf Bild zeichnen. Ich kann ein anderes Bild oder eine andere Ansicht auf das Bild legen, das kein Problem für mich ist, aber wie zeichne ich das? – GameLoading

Antwort

1
-(IBAction)Handeltap:(UIGestureRecognizer *)sender 
{ 


    if(sender.state==UIGestureRecognizerStateBegan) 
    { 
     tapPoint1 = [sender locationInView:sender.view]; 
     NSLog(@"UIGestureRecognizerStateBegan and x=%d and y=%d",(int)tapPoint1.x,(int)tapPoint1.y); 
     img1=[[UIImageView alloc]initWithImage:nil]; 
     [img1 setBackgroundColor:[UIColor lightTextColor]]; 
     CGRect rect1=CGRectMake((float)tapPoint1.x,(float)tapPoint1.y,50,20); 
     NSLog(@"rect=%f and %f and %f and %f",rect1.origin.x,rect1.origin.y,rect1.size.width,rect1.size.height); 
     [img1 setFrame:rect1]; 
     [self.view addSubview:img1]; 
     [self.view bringSubviewToFront:img1]; 

    } 
    if(sender.state==UIGestureRecognizerStateChanged) 
    {tapPoint2 = [sender locationInView:sender.view]; 
//  NSLog(@"UIGestureRecognizerStateChanged and x=%d and y=%d",(int)tapPoint.x,(int)tapPoint.y);  
     [img1 setFrame:CGRectMake(img1.frame.origin.x,img1.frame.origin.y,(float)tapPoint2.x-img1.frame.origin.x,(float)tapPoint2.y-img1.frame.origin.y)]; 
} 
    if(sender.state==UIGestureRecognizerStateEnded) 
    { 
     tapPoint2 = [sender locationInView:sender.view]; 
     NSLog(@"UIGestureRecognizerStateEnded and x=%d and y=%d",(int)tapPoint2.x,(int)tapPoint2.y); 



     [img1 setFrame:CGRectMake(img1.frame.origin.x,img1.frame.origin.y,(float)tapPoint2.x-img1.frame.origin.x,(float)tapPoint2.y-img1.frame.origin.y)]; 

    } 

} 

Tippgeste hinzufügen, und fügen Sie diese

UILongPressGestureRecognizer *ges11=[[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(Handeltap:)]; 

    [self.view addGestureRecognizer:ges11]; 

Tanks für die Hilfe ..

1

Beginnen Sie mit dem Lesen Quartz 2D Programming Guide und gehen Sie zum Abschnitt Pfade.

+0

Ich bin nicht in der Lage, den Code von dort zu bekommen. bitte helfen Sie mir .. – GameLoading

0

Wenn Sie nur ein einfaches farbiges Rechteck in ein Bild hinzufügen möchten, können Sie es wie folgt tun:

UIImage *imageFile = [[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"myImageFile" ofType:@"jpg"]]; 
    UIImageView *myImageFileView = [[UIImageView alloc] initWithImage:imageFile]; 
    myImageFileView.frame = CGRectMake(0, 0, 320, 480); //size of image view 
    [self.view addsubview:myImageFileView]; //I assume this is within a view controller .m 
UIView *rectangle = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)]; //A rectangle at point (0, 0) 50x50 in size. 
rectangle.backgroundColor = [UIColor redColor]; //color the rectangle 
[myImage addSubview:rectangle]; //add the rectangle to your image 
[rectangle release]; 
[imageFile release]; 
[myImageFileView release]; 
+0

eigentlich habe ich ein Bild auf der Aussicht. jetzt möchte ich ein transparentes Rechteck auf diesem Bild zeichnen. – GameLoading

+0

rectangle.backgroundColor = [UIColor clearcolor]; – Zigglzworth