By using CGPoint location it is always saving the last image in uiscrollview. When i m tapping on other image to save. What can i do to save the exact image one i tapped.
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);
}}}
Any ideas will be appreciated.
Thanks
The point will always appear within the bounds of the
UIScrollView
in which theLongPressGestureRecognizer
is triggered. You should check your scroll view'scontentOffset
(usecontentOffset.x
for horizontal layouts andcontentOffset.y
for vertical layouts) to detect which image you should save.Additionally you could convert the touch point to the
UIImageView
instance's local coordinate system and see if the the point lies within the image view'sbounds
rect.UPDATE
For example you could use something like this to detect if the point is within the image view's bounds (note: I have not tested this and this is assuming there is more than one image view added to the scroll view):
You should also consider detecting which image should be saved before and storing a reference to that image before displaying the
UIActionSheet
to the user as it may decrease the number of potential issues you might encounter and will be easier to read later, but this is my subjective opinion.