Search code examples
iphoneiosipaduiviewuiimageview

uiimage not being put into subview correctly


This is a difficult problem to explain... but i'll do my best. First a background on the problem, basically i am creating a paint like app for ios and wanted to add a functionality that allows the user to select part of the image (multi-touch shows an opaque rectangle) and delete/copy-paste/rotate that part. I have got the delete and copy-paste working perfectly but the rotation is another story. To rotate the part of the image I first start by copying the part of the image and setting it to be the background of the selected rectangle layer, then the user rotates by an arbitrary angle using a slider. The problem is that sometimes the image ends up being displayed from another location of the rectangle (meaning the copied image hangs off the wrong corner of the rectangle). I thought this could be a problem with my rectangle.frame.origin but the value for that seems to be correct through various tests. It also seems to change depending on the direction that the drag goes in...

These Are Screens of the problem

1

2

3

In each of the above cases the mismatched part of the image should be inside the grey rectangle, i am at a loss as to what the problem is.

     bg = [[UIImageView alloc] initWithImage:[self crop:rectangle.frame:drawImage.image]];
     [rectangle addSubview:bg];

drawImage is the users drawing, and rectangle is the selected grey area. crop is a method which returns a part of a given image from a give rect.

I am also having trouble with pasting an arbitrarily rotated image.. any ideas on how to do that?

Edit: adding more code.

    -(void)drawRect:(int)x1:(int)y1:(int)x2:(int)y2{
     [rectangle removeFromSuperview];
     rectangle = [[UIView alloc] initWithFrame:CGRectMake(x1, y1, x2-x1, y2-y1)]; 
     rectangle.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.6];
     selectionImage = drawImage.image;
     drawImage.image = selectionImage;
     [drawImage addSubview:rectangle];
     rectangleVisible = true;
     rectangle.transform = transformation;

Could it have anything to do with how i draw my rectangle? (above) I call this method from a part of a touchesMoved method (below) which may cause the problem (touch 1 being in the wrong location may cause width to be negative?) if so, is there an easy way to remedy this?

     if([[event allTouches] count] == 2 && !drawImage.hidden){
        NSSet *allTouches = [event allTouches];
        UITouch *touch1 = [[allTouches allObjects] objectAtIndex:0];
        UITouch *touch2 = [[allTouches allObjects] objectAtIndex:1];
        [self drawRect:[touch1 locationInView:drawImage].x :[touch1 locationInView:drawImage].y:
        [touch2 locationInView:drawImage].x :[touch2 locationInView:drawImage].y];   
     }

Solution

  • I'm not sure if this is your problem, but it looks like you are just assuming that touch1 represents the upper left touch. I would start out by standardizing the rectangle.

    // Standardizing the rectangle before making it the frame.
    CGRect frame = CGRectStandardize(CGRectMake(x1, y1, x2-x1, y2-y1));
    rectangle = [[UIView alloc] initWithFrame:frame];