0

我想做的是将当前图像从滚动视图保存到相册。尝试了很多不同的方法,它总是将最后一张图片从滚动视图保存到相册。没有得到我在代码中缺少的东西。

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

    }

编辑:

我试过这个howcome这也不起作用

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

感谢帮助。

谢谢

4

1 回答 1

2

您有 2-3 个选项可以这样做。会告诉你这个选项对我来说非常有效。

只需继承 UIScrollView 类,本地化您的 UIImageView 对象(意味着在 for 循环本身中声明它。),将标签分配给每个图像视图。使 UIImageView 启用 userInteraction 否则它不会检测到触摸,然后在 UIScrollView 子类和当前类中编写 touchesBegan 方法。然后在处理 GestureRecognizer 时编写任何你想要的代码。然后从 UIScrollView 的 touchesBegan 方法调用当前类的 touchesBegan。

尝试自己,学习自己。

于 2012-10-26T05:43:21.880 回答