Reputation: 767
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
Upvotes: 0
Views: 176
Reputation: 3515
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.
Upvotes: 2