2

I have an application having functionality for image clipping. The Functionality is I have three images :

  • Main image - set as Background
  • Overlay image - image set on Main image
  • Pointer image - image that will move on touch

I want to remove the Overlay image on touch as i drag the Pointer image. After clipping Overlay image Main image should get visible.

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {
    // Retrieve the touch point
    CGPoint pt = [[touches anyObject] locationInView:imgPointer];
    startLocation = pt;
    [[imgPointer superview] bringSubviewToFront:imgPointer];
}
- (void) touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event {
    // Move relative to the original touch point
    CGPoint pt = [[touches anyObject] locationInView:imgPointer];
    CGRect frame = [imgPointer frame];
    frame.origin.x += pt.x - startLocation.x;
    frame.origin.y += pt.y - startLocation.y;
    [imgPointer setFrame:frame];

    UIGraphicsBeginImageContext(imgPointer.frame.size);

    CGContextRef currentContext = UIGraphicsGetCurrentContext();
    CGRect clippedRect = CGRectMake(0, 0, imgPointer.frame.size.width, imgPointer.frame.size.height);
    CGContextClipToRect( currentContext, clippedRect);
    CGRect drawRect = CGRectMake(imgPointer.frame.origin.x * -1,
                                 imgPointer.frame.origin.y * -1,
                                 imgOverlay.frame.size.width,
                                 imgOverlay.frame.size.height);

    //draw the image to our clipped context using our offset rect

    CGContextDrawImage(currentContext, drawRect, imgOverlay.image.CGImage);

    //pull the image from our cropped context

    UIImage *cropped = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

}

From using above code i am not able to clip Overlay image.

Thank you.

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
Pankti Patel
  • 296
  • 1
  • 4
  • 14
  • You are creating a new image on every `touchesmoved` event. First off that is a very bad idea. It will create a new image possibly tens of times per second. Very very bad for a responsive UI. Second, you are not doing anything with that image, You just create it and then it gets destroyed with the end of the function. My suggestion would be to do the new image at the `touchesended` and then assign it to something on screen. – Putz1103 Dec 20 '13 at 14:26
  • @Putz1103 Can you please give me example or some link according to your idea?. I have not used the image object because i don't understand how to remove that object from image. – Pankti Patel Dec 21 '13 at 04:46
  • I can't say I know exactly what you are doing. But the way I would do cropping is to draw a rectangle over the image you are trying to crop and modify that rectangle on touchesmoved (much less processor intensive). Then on touches ended I would make a new image (like you are doing) and assign it to some UIImageView on the screen. I don't have any links to offer you, but that would be my general design. If you don't know how to modify a UIImageView you can research that and it should help you quite a bit. – Putz1103 Dec 21 '13 at 15:40

0 Answers0