Contentoffset for Tapped imageview in scrollview

393 Views Asked by At

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.

2

There are 2 best solutions below

4
On

The reason is in your viewDidLoad you are using

_image = [UIImage imageNamed:imageName];

I think this is a global variable in the class, and you are using this object to save the image to photo album. As it is a global variable in the last count of your loop's image will be pointing to it, so you are always getting last image saved.

You can fix it by making _image local within for loop. And as you are setting the tag, using the tag you can get the imageView/image from scrollView. You can have like this

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

This should work for you.

1
On
  int imageView = floor((Offset.x - imageView / 320) / imageView) + 1;

By this line

 int imageView = (int)(scrollView.contentOffset.x / scrollView.frame.size.width);