裁剪ImageView生成新图像

时间:2014-04-30 21:37:06

标签: ios

每当用户触摸屏幕上的UIImageView时,我都会尝试裁剪图像。 UIImageView是640 X 300区域,我允许用户触摸UIImageView中的任何位置。然后我使用以下代码来查看croppedImage,它总是显示错误的图像。我想我无法获得正确的坐标。

-(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint location = [[touches anyObject] locationInView:self.view];

    UIImage *originalImage = self.imageView.image;

    NSLog(@"x = %f, y = %f",location.x,location.y);

    CGRect cropRegion = CGRectMake(location.x, location.y, 10, 10);

    CGImageRef subImage = CGImageCreateWithImageInRect(originalImage.CGImage, cropRegion);

    UIImage *croppedImage = [UIImage imageWithCGImage:subImage];
}

1 个答案:

答案 0 :(得分:0)

您将获得相对于self.view的坐标,而不是相对于图像视图的坐标。尝试:

CGPoint location = [[touches anyObject] locationInView:self.imageView];