1

I'm trying to create a photo based app and I want to be able to crop, rotate and move the image thats been taken. I've done those things already but the UIImage created moves around the whole UIViewController and I only want it to be moved around within its UIImageView. Could anyone suggest how I might go about trying this? I've tried a few way already but they don't seemed to have worked. Any suggestions would be really helpful. Thanks!

Update

I'm adding in my code to help better explain what I'm trying to do . I'm sorry for not doing this before.

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
//the 'image' below is the UIImage, it has already been declared before hand
image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

//layoutOne is my UIView which holds my UIImageView and UIImage. all of those are declared in my .h file.

layoutOne = [[UIView alloc] initWithFrame:CGRectMake(95.0, 107.0, 578, 682)];
theImageView = [[UIImageView alloc] initWithFrame:[layoutOne frame]];
[theImageView setImage:image];
[layoutOne addSubview:theImageView];

UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
[pinchRecognizer setDelegate:self];
[layoutOne addGestureRecognizer:pinchRecognizer];

UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
[rotationRecognizer setDelegate:self];
[layoutOne addGestureRecognizer:rotationRecognizer];

UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
[panRecognizer setMinimumNumberOfTouches:1];
[panRecognizer setMaximumNumberOfTouches:1];
[panRecognizer setDelegate:self];
[layoutOne addGestureRecognizer:panRecognizer];


UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setDelegate:self];
[layoutOne addGestureRecognizer:tapRecognizer];

[self.view addSubview:layoutOne];
}

I'm using Interface Builder to create my app and I'm also using the newest version of Xcode. Thought I'd also add this incase it helped. Thanks

Ollie177
  • 315
  • 1
  • 3
  • 17

2 Answers2

1

Not sure what your current setup is, but make sure your UIImageView clipsToBounds property is set to YES.

If this doesn't answer your question, try checking the answers to Cropping an UIImage, which sounds like it may be describing a similar problem.

Community
  • 1
  • 1
Mike Fahy
  • 5,487
  • 4
  • 24
  • 28
  • Thanks for the quick response. I tried that but for some reason when I added it in, like this: **[theImageView clipsToBounds:YES];** It comes up with the semantic issue: **Instance method '-clipsToBounds:' not found (return type defaults to 'id')** – Ollie177 Dec 06 '11 at 16:22
  • Sounds like theImageView is not recognized as an `UIImageView` object. Have you passed the object into a method as an `id` instance? If so, try providing the compiler with the correct type information before sending the clipsToBound: message: `[(UIImageView *) theImageView clipsToBounds:YES];` – Mike Fahy Dec 06 '11 at 16:34
  • I think what you said was right, however you mentioned you didn't know my setup. To help, i'll post my method of displaying the UIImageView and hopefully it will clarify I should have done that before really. Sorry about that. I'll add in my code above – Ollie177 Dec 07 '11 at 14:28
1

I don't think you can move the image within the image view. What you can to is to create a UIView as the frame and move the image within the bounds of the frame. Below is the code snippet (iOS 5 with ARC) that I hope it helps.

@implementation ImageFrame
{
    UIImageView *imageView;
    CGPoint previousPoint;
}

- (id)initWithFrame:(CGRect)frame andImage:(UIImage *)image
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor greenColor];
        self.clipsToBounds = YES;
        imageView = [[UIImageView alloc] initWithImage:image];
    }
    return self;
}

- (void)layoutSubviews
{
    [self addSubview:imageView];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    previousPoint = [[touches anyObject] locationInView:self];
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    CGPoint location = [touch locationInView:self];
    CGPoint movePoint = CGPointMake(location.x - previousPoint.x, location.y - previousPoint.y);
    CGPoint newCenter = CGPointMake(imageView.center.x + movePoint.x, imageView.center.y + movePoint.y);
    imageView.center = newCenter;
    previousPoint = location;
}

@end
Ken W
  • 999
  • 7
  • 10