0

I am trying to save the tapped image to a photo album, using contentOffset to detect which image object is tapped to save, but it always saves the last imageObject instead.

Here is how I try to calculate contentOffset for tapped image view in scrollView:

(void)viewDidLoad

{
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
imageScrollView.delegate = self;
imageScrollView.pagingEnabled = YES;
for (int i = 0; i < 61; i++) {

    CGFloat xOrigin = i * 320;


    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];

    [myButton addTarget:self action:@selector(dismissView:) forControlEvents:UIControlEventTouchUpInside];

    myButton.frame = CGRectMake(xOrigin, 10, 60, 35);


    [myButton.layer setMasksToBounds:YES];

    [myButton.layer setCornerRadius:10.0f];

    myButton.layer.borderWidth = 2;

    myButton.layer.borderColor = [[UIColor whiteColor] CGColor];

    [myButton setTitle:@"Done" forState:UIControlStateNormal];

    myButton.backgroundColor = [UIColor clearColor];

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

UIImage *image = [UIImage imageNamed:imageName];

    _imageView = [[[UIImageView alloc] initWithImage:image]autorelease]; 
     _imageView.frame = CGRectMake(xOrigin, 0, 320, 480);

    _imageView.tag = i;

    [_imageScrollView viewWithTag:i+1];

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

    imageScrollView.userInteractionEnabled = YES;

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

    [imageScrollView addSubview:_imageView];

    [imageScrollView addSubview:myButton];


}

imageScrollView.contentSize = CGSizeMake(320 * 61 , 480);


[self.view addSubview:imageScrollView];

  }

 - (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 performSelector:@selector(LongPress:) withObject:nil];

       break;

    default:
        break;

}}

- (void)LongPress:(UILongPressGestureRecognizer*)gestureRecognizer{

CGPoint offset = _imageScrollView.contentOffset;

int imageView = floor((Offset.x - imageView / 320) / imageView) + 1;
UIImage* image = [(UIImageView*)[_imageScrollView viewWithTag:imageView] image];

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

I expected this code to save the tapped image, but it saves the last image view from the scrollview instead.

Please let me know if I am doing it incorrectly, and if I am still missing something. Thanks for the help.

4

2 回答 2

1

原因在于viewDidLoad您正在使用

_image = [UIImage imageNamed:imageName];

我认为这是类中的全局变量,您正在使用此对象将图像保存到相册。由于它是循环图像的最后计数中的全局变量,因此将指向它,因此您始终会保存最后一张图像。

_image您可以通过在 for 循环中进行本地化来修复它。在设置标签时,使用标签可以从 scrollView 获取 imageView/image。你可以有这样的

//your imageView in longPress function has the selected imageView's tag
UIImage* selImage = [(UIImageView*)[imageScrollView viewWithTag:imageView] image];

这应该适合你。

于 2012-10-21T16:48:08.273 回答
1
  int imageView = floor((Offset.x - imageView / 320) / imageView) + 1;

通过这条线

 int imageView = (int)(scrollView.contentOffset.x / scrollView.frame.size.width);
于 2012-10-21T17:16:57.510 回答