1

通过使用 CGPoint 位置,它总是将最后一张图像保存在 uiscrollview 中。当我点击其他图像进行保存时。我该怎么做才能保存我点击的确切图像。

UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.pagingEnabled = YES;

NSInteger numberOfViews = 61;

for (int i = 0; i < numberOfViews; i++) {

    CGFloat xOrigin = i * self.view.frame.size.width;

 NSString *imageName = [NSString stringWithFormat:@"image%d.png", i];

    _image = [UIImage imageNamed:imageName];

    _imageView = [[UIImageView alloc] initWithImage:_image];

    _imageView.frame = CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height);

 UILongPressGestureRecognizer *gestureRecognizer = [[UILongPressGestureRecognizer alloc]
                                                       initWithTarget:self
                                                       action:@selector(handleLongPress:)];

    imageScrollView.userInteractionEnabled = YES;
    [imageScrollView addGestureRecognizer:gestureRecognizer];
    gestureRecognizer.delegate = self;
    [gestureRecognizer release];

    [imageScrollView addSubview:_imageView];
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);

    - (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateBegan){
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Save Photo", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
    [actionSheet showInView:self.view];
    [actionSheet release];

     }}

 -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {

switch (buttonIndex) {
    case 0:
        [self savePhoto];

       break;

    default:
        break;

}

   -(void)savePhoto{

CGPoint location = [gesture locationInView:_imageView];

if  (CGRectContainsPoint(_imageView.bounds, location)){

UIImageWriteToSavedPhotosAlbum(_image, self, @selector(image: didFinishSavingWithError:contextInfo:), nil);
   }}}

任何想法将不胜感激。

谢谢

4

1 回答 1

1

该点将始终出现在触发 的范围UIScrollViewLongPressGestureRecognizer。您应该检查滚动视图contentOffsetcontentOffset.x用于水平布局和contentOffset.y垂直布局)以检测应该保存的图像。

此外,您可以将触摸点转换为UIImageView实例的本地坐标系,并查看该点是否位于图像视图的bounds矩形内。

更新

例如,您可以使用类似的方法来检测该点是否在图像视图的范围内(注意:我没有对此进行测试,这是假设滚动视图中添加了多个图像视图):

if (CGRectContainsPoint(_imageView.bounds, [self.view convertPoint:location toView:_imageView]))
{
    // do something
}

您还应该考虑检测之前应该保存哪个图像并在UIActionSheet向用户显示之前存储对该图像的引用,因为它可能会减少您可能遇到的潜在问题的数量并且以后更容易阅读,但这是我的主观意见.

于 2012-10-18T20:19:57.660 回答