确定滚动视图当前所处的图像

时间:2012-10-19 21:56:43

标签: iphone uiscrollview ios6

尝试锻炼当前打开的图像滚动视图以将其保存到照片库。 它还没有工作。

 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;
 }

不知道我遗漏了哪些重要代码。已删除scrollview,使用imagescrollview.delegate = self;并且在scrollviewdidscroll方法中使用contentoffset来跟踪当前位置,但仍然没有保存当前图像。

非常感谢帮助。

由于

0 个答案:

没有答案