Tracing the exact location of the longpressgesture with CGPoint - iphone

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 the LongPressGestureRecognizer is triggered. You should check your scroll view's contentOffset (use contentOffset.x for horizontal layouts and contentOffset.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's bounds 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):
if (CGRectContainsPoint(_imageView.bounds, [self.view convertPoint:location toView:_imageView]))
{
// do something
}
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.

Related

Get the current image in the scrollview

I m trying to do is to save the current image from the scrollview to photo album. Tried so many different ways and it always saves last image from the scrollview to photo album. Not getting what i m missing in the code.
- (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;
pictures = [[NSArray alloc] initWithObjects:#"image0.png", #"image1.png", #"image2.png", #"image3.png", #"image4.png", #"image5.png", #"image6.png", #"image7.png", #"image8.png", #"image9.png", #"image10.png", #"image11.png", #"image12.png", #"image13.png", #"image14.png", #"image15.png", #"image16.png", #"image17.png", #"image18.png", #"image19.png", #"image20.png", #"image21.png", #"image22.png", #"image23.png", #"image24.png", #"image25.png", #"image26.png", #"image27.png", #"image28.png", #"image29.png", #"image30.png", #"image31.png", #"image32.png", #"image33.png", #"image34.png", #"image35.png", #"image36.png", #"image37.png", #"image38.png", #"image39.png", #"image40.png", #"image41.png", #"image42.png", #"image43.png", #"image44.png", #"image45.png", #"image46.png", #"image47.png", #"image48.png", #"image49.png", #"image50.png", #"image51.png", #"image52.png", #"image53.png", #"image54.png", #"image55.png", #"image56.png", #"image57.png", #"image58.png", #"image59.png", #"image60.png", nil];
_image = [UIImage imageNamed:[pictures objectAtIndex:i]];
_imageView = [[[UIImageView alloc] initWithImage:_image]autorelease];
_imageView.tag = i+1;
_imageView.image = _image;
_imageView.frame = CGRectMake(xOrigin, 0, 320, 480);
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{
NSInteger currentIndex = roundf(_imageScrollView.contentOffset.x / 320) + 1;
//UIImageView *currentImage = [_imageScrollView viewWithTag:currentIndex];
UIImage* currentImage = [(UIImageView*)[_imageScrollView viewWithTag:currentIndex] image];
UIImageWriteToSavedPhotosAlbum(_imageView.image, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
}
Edit:
I tried this howcome this is not working either
- (void)LongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
if([pictures objectAtIndex:0]) {
UIImageWriteToSavedPhotosAlbum(_imageView.image, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
} else if([pictures objectAtIndex:1]) {
UIImageWriteToSavedPhotosAlbum(_imageView.image, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
}
Appreciate help.
Thanks
You have 2-3 options to do so. WIll tell you the option worked perfectly for me.
Just subclass the UIScrollView class, localize your UIImageView object (means declare it in for loop itself.), assign the tag to each imageview. make userInteraction enabled to UIImageView otherwise it won't detect touches, then write touchesBegan method in UIScrollView subclass & in current class. Then write the code whatever you want in handling GestureRecognizer. And then call the touchesBegan of current class from touchesBegan method of UIScrollView.
Try yourself, learn yourself.

Why it is not detecting which image I'm tapping on [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Tracing the exact location of the longpressgesture with CGPoint
It always gives NSLog for image pressed 0. I'm don't understand what it is exactly what I'm doing wrong. It never gives me the number of the image I'm pressing. Even when I try to save the image I tap on it always saves the very last image.
- (void)viewDidLoad
{
UIScrollView *imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
imageScrollView.delegate = self;
imageScrollView.pagingEnabled = YES;
for (int i = 0; i < 61; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
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];
_image = [UIImage imageNamed:imageName];
_imageView = [[[UIImageView alloc] initWithImage:_image]autorelease];
_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 addSubview:myButton];
}
imageScrollView.contentSize = CGSizeMake(_imageView.frame.size.width * 61 , _imageView.frame.size.height);
[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{
UIView *view = gestureRecognizer.view;
int index = view.tag;
//UIImageWriteToSavedPhotosAlbum(index, self, #selector(image: didFinishSavingWithError:contextInfo:), nil);
NSLog(#"image pressed %i", index);
}
Because you never set the tag for the view so it defaults to 0. And seriously, how many times are you going to ask the same question? You've been given quite a lot of good feedback, use it.
Tracing the exact location of the longpressgesture with CGPoint
contentoffset in scrollview
Work out what image the scroll view is currently on
detect image object user is viewing in scrollviewdidscroll

Save Images from ImageScrollView in photo library

How i can save images from this scrollview in photo library when user longpress on imageview.
Getting yellow warning for statement
UIImageWriteToSavedPhotosAlbum(_imageScrollView, self, nil, nil);
How can i resolve this warning to save image on photo library
- (void)viewDidLoad
{ self.view.backgroundColor = [UIColor blackColor];
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];
UIImage *image = [UIImage imageNamed:imageName];
UIImageView *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:)];
gestureRecognizer.minimumPressDuration = 2.0;
imageScrollView.userInteractionEnabled = YES;
[imageScrollView addGestureRecognizer:gestureRecognizer];
gestureRecognizer.delegate = self;
[gestureRecognizer release];
[imageScrollView addSubview:imageView];
[imageScrollView addSubview:myButton];
}
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:imageScrollView];
[imageScrollView release];
}
- (void)handleLongPress:(UILongPressGestureRecognizer*)gestureRecognizer{
if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil otherButtonTitles:#"Save Photo", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
[actionSheet showInView:self.view];
[actionSheet release];
}
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0:
UIImageWriteToSavedPhotosAlbum(_imageScrollView, self, nil, nil);
break;
default:
break;
}
}
Getting warning in yellow on this statement
UIImageWriteToSavedPhotosAlbum(_imageScrollView, self, nil, nil);
that incompatible pointer types passing UIScrollView to parameter of type UIImage
Thanks
see
this : saving image
and
this :download and saving image
but this one have great rating prev. answer

Contentoffset for Tapped imageview in scrollview

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.
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.
int imageView = floor((Offset.x - imageView / 320) / imageView) + 1;
By this line
int imageView = (int)(scrollView.contentOffset.x / scrollView.frame.size.width);

contentoffset in scrollview

How can i set the contentoffset for image to track down which image user is on and selected to save it to phot album.
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);
Edit:
imageScrollView.contentOffset = CGPointMake(CGFloat x, CGFloat y);
in the contentoffset not sure what to put so that image can be tracked the user is on and selected to save to photo album
- (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.
You can use this code (that comes from one of the apple sample projects for scrollview) to determine what the currently visible 'page' is in the scrollview.
// Calculate which page is visible
CGFloat pageWidth = self.scrollView.frame.size.width;
int page = floor((self.scrollView.contentOffset.x - pageWidth / 2) / pageWidth) + 1;
Once you know this index number you can use it to determine what image to save