如何防止UIImage被移动到其UIImageView之外?

时间:2011-12-06 14:35:15

标签: objective-c uiviewcontroller uiimageview uiimage photo

我正在尝试制作基于照片的应用,我希望能够裁剪,旋转和移动拍摄的图像。我已经完成了那些事情,但创建的UIImage在整个UIViewController中移动,我只希望它在UIImageView中移动。任何人都可以建议我如何尝试这个?我已经尝试了一些方法,但他们似乎没有工作。任何建议都会非常有用。谢谢!

更新

我添加了我的代码,以帮助更好地解释我尝试做的事情。我很抱歉没有这样做过。

- (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];
}

我使用Interface Builder创建我的应用程序,我也使用最新版本的Xcode。我以为我也加了这个帮助。感谢

2 个答案:

答案 0 :(得分:1)

不确定您当前的设置是什么,但要确保您的UIImageView clipsToBounds属性设置为YES.

如果这不能解答您的问题,请尝试检查Cropping an UIImage的答案,这听起来似乎可能描述了类似的问题。

答案 1 :(得分:0)

我认为你不能在图像视图中移动图像。你可以做的是创建一个UIView作为框架,并在框架的边界内移动图像。下面是我希望它有帮助的代码片段(带有ARC的iOS 5)。

@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
相关问题