在scrollview中获取当前图像

时间:2012-10-25 18:21:30

标签: iphone indexing ios6 scrollview

我要做的是将当前图像从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;

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

    }

编辑:

我试过这个结果,这不起作用

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

感谢帮助。

由于

1 个答案:

答案 0 :(得分:2)

您有2-3个选项可以执行此操作。我会告诉你这个选项对我来说非常合适。

只是继承UIScrollView类,本地化你的UIImageView对象(意味着将它声明为for循环本身。),将标记分配给每个imageview。使userInteraction启用UIImageView,否则它不会检测到触摸,然后在UIScrollView子类中写入touchesBegan方法&amp;在当前课程。然后在处理GestureRecognizer时编写代码。然后从UIScrollView的touchesBegan方法调用当前类的touchesBegan。

尝试自己,自学。