Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8381795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:46:33+00:00 2026-06-09T16:46:33+00:00

I have been stuck for days on something that must be pretty simple, I’ve

  • 0

I have been stuck for days on something that must be pretty simple, I’ve searched for a few days but I cant seem to get the right code to work.

I’ve two images, a background image and another image that will mask the first one. I can move the masked image with gestures. I can scale it, rotate it and move it.

http://i45.tinypic.com/dyq2k8.png

When I press the test button I want her face to get cropped/masked into another UIImage from the current location of the masked image.

Any thoughts on what I should do?

Help would be really, really nice 🙂
thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:46:35+00:00Added an answer on June 9, 2026 at 4:46 pm

    😛

    Funny that. I had wrote a demo application a while back which does everything you want, pinch zoom, pan photo.

    You will have to provide your own image as I’ve only copied the source, but here it is:

    // ------------------------------------------------
    // view controller header file
    // ------------------------------------------------
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController <UIGestureRecognizerDelegate>
    {
        UIButton *btnEdit;
        BOOL isEditing;
        UIImageView *displayImage;
        UIImageView *photoView;
        UIImageView *maskView;
    }
    
    @end
    
    
    
    
    
    // ------------------------------------------------    
    // view controller implementation file
    // ------------------------------------------------
    
    #import "ViewController.h"
    #import <QuartzCore/QuartzCore.h>
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    
        isEditing = false;
    
        photoView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
        [photoView setImage:[UIImage imageNamed:@"photo.png"]];
        photoView.hidden = YES;
    
        maskView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
        [maskView setImage:[UIImage imageNamed:@"maskguide.png"]];
        maskView.hidden = YES;
    
        displayImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
        UIPinchGestureRecognizer *pinchGesture = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];
    
        [panGesture setDelegate:self];
        [pinchGesture setDelegate:self];
    
        [photoView addGestureRecognizer:panGesture];
        [photoView addGestureRecognizer:pinchGesture];
        [photoView setUserInteractionEnabled:YES];
    
        [panGesture release];
        [pinchGesture release];
    
        btnEdit = [[UIButton alloc] initWithFrame:CGRectMake(60, 400, 200, 50)];
        [btnEdit setBackgroundColor:[UIColor blackColor]];
        [btnEdit setTitle:@"Start Editing" forState:UIControlStateNormal];
        [btnEdit addTarget:self action:@selector(toggleEditing) forControlEvents:UIControlEventTouchUpInside];
    
        [[self view] addSubview:displayImage];
        [[self view] addSubview:photoView];
        [[self view] addSubview:maskView];
        [[self view] addSubview:btnEdit];
    
        [self updateMaskedImage];
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
    }
    
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
        return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
    }
    
    -(void)dealloc
    {
        [btnEdit release];
    
        [super dealloc];
    }
    
    #pragma mark -
    #pragma mark Update Masked Image Method
    #pragma mark -
    
    -(void)updateMaskedImage
    {
        maskView.hidden = YES;
    
        UIImage *finalImage = 
        [self maskImage:[self captureView:self.view]
               withMask:[UIImage imageNamed:@"mask.png"]];
    
    
        maskView.hidden = NO;
    
        //UIImage *finalImage = [self maskImage:photoView.image withMask:[UIImage imageNamed:@"mask.png"]];
    
        [displayImage setImage:finalImage];
    }
    
    - (UIImage*) maskImage:(UIImage *)image withMask:(UIImage *)maskImage {
    
        CGImageRef maskRef = maskImage.CGImage; 
    
        CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
                                            CGImageGetHeight(maskRef),
                                            CGImageGetBitsPerComponent(maskRef),
                                            CGImageGetBitsPerPixel(maskRef),
                                            CGImageGetBytesPerRow(maskRef),
                                            CGImageGetDataProvider(maskRef), NULL, false);
    
        CGImageRef masked = CGImageCreateWithMask([image CGImage], mask);
        return [UIImage imageWithCGImage:masked];
    
    }
    
    #pragma mark -
    #pragma mark Touches Began
    #pragma mark -
    
    // adjusts the editing flag to make dragging and drop work
    -(void)toggleEditing
    {
        if(!isEditing)
        {
            isEditing = true;
    
            NSLog(@"editing...");
    
            [btnEdit setTitle:@"Stop Editing" forState:UIControlStateNormal];
    
            displayImage.hidden = YES;
            photoView.hidden = NO;
            maskView.hidden = NO;
        }
        else
        {
            isEditing = false;
    
            [self updateMaskedImage];
    
            NSLog(@"stopped editting");
    
            [btnEdit setTitle:@"Start Editing" forState:UIControlStateNormal];
    
            displayImage.hidden = NO;
            photoView.hidden = YES;
            maskView.hidden = YES;
        }
    }
    
    /*
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {   
        if(isEditing)
        {
            UITouch *finger = [touches anyObject];
            CGPoint currentPosition = [finger locationInView:self.view];
    
            //[maskView setCenter:currentPosition];
            //[photoView setCenter:currentPosition];
            if([touches count] == 1)
            {
                [photoView setCenter:currentPosition];
            }
            else if([touches count] == 2)
            {
    
            }
        }
    }
    */
    
    -(void)handlePan:(UIPanGestureRecognizer *)recognizer
    {    
        CGPoint translation = [recognizer translationInView:self.view];
        recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x, 
                                             recognizer.view.center.y + translation.y);
        [recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
    }
    
    -(void)handlePinch:(UIPinchGestureRecognizer *)recognizer
    {    
        recognizer.view.transform = CGAffineTransformScale(recognizer.view.transform, recognizer.scale, recognizer.scale);
        recognizer.scale = 1;
    }
    
    -(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
    {
        return YES;
    }
    
    #pragma mark -
    #pragma mark Capture Screen Function
    #pragma mark -
    
    - (UIImage*)captureView:(UIView *)yourView 
    {
        UIGraphicsBeginImageContextWithOptions(yourView.bounds.size, yourView.opaque, 0.0);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [yourView.layer renderInContext:context];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    
    #pragma mark -
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

a very simple question I am afraid but I have been stuck for days
I have been stuck on this issue for a few days now and seem
I have been stuck for a long time on this simple but obtuse message
I have been stuck on this for a few days now and it is
I'm pretty sure someone will have done something like this before but have been
I have been stuck on this one for days, but I have broken it
I have been stuck on this for a few days now. I am attempting
I have been stuck on this for a few days and I would ping
I've been stuck on this for a few days now and can't seem to
Have been stuck with this issue for a few days now, and really need,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.