滚动视图中的Tapped imageview的Contentoffset

时间:2012-10-21 16:17:09

标签: iphone imageview ios6 scrollview contentoffset

我正在尝试将点按的图像保存到相册,使用contentOffset来检测点击要保存的图像对象,但它总是保存最后一个imageObject。

以下是我在scrollView中尝试计算tapped图像视图的contentOffset的方法:

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

我希望此代码能够保存已点按的图片,但它会保留滚动视图中的最后一个图片视图。

如果我做得不对,请告诉我,如果我仍然遗失某些东西。谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

原因在于您正在使用的viewDidLoad

_image = [UIImage imageNamed:imageName];

我认为这是班级中的全局变量,您正在使用此对象将图像保存到相册。由于它是循环最后一次计数的全局变量,因此图像将指向它,因此您始终会保存最后一张图像。

您可以通过for循环中的_image本地修复它。当您设置标签时,使用标签可以从scrollView获取imageView / image。你可以喜欢这个

//your imageView in longPress function has the selected imageView's tag
UIImage* selImage = [(UIImageView*)[imageScrollView viewWithTag:imageView] image];

这应该适合你。

答案 1 :(得分:1)

  int imageView = floor((Offset.x - imageView / 320) / imageView) + 1;

通过这一行

 int imageView = (int)(scrollView.contentOffset.x / scrollView.frame.size.width);