Work out what image the scroll view is currently o

2019-09-07 10:22发布

问题:

Trying to workout that what image scrollview is currently on to save it to photo library. It is not working still.

 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.delegate = self;
 [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, [self.view convertPoint:location toView:_imageView]))
{

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

- (void)scrollViewDidScroll:(UIScrollView *)sender {

CGFloat pageWidth = _imageScrollView.frame.size.width;

NSInteger page = (NSInteger)floor((self.imageScrollView.contentOffset.x * 2.0f + pageWidth) / (pageWidth * 2.0f));

self.pageControl.currentPage = page;
 }

Have no idea what important piece of code i m missing.Delegated scrollview, used imagescrollview.delegate=self; and in the scrollviewdidscroll method used contentoffset to track the current location but still it is not saving current image.

Help will be really appreciated.

Thanks