2012-10-25 58 views
0

我試圖做的是將當前圖像從滾動視圖保存到相冊中。嘗試了很多不同的方式,它總是將最後一張圖像從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); 

    } 

編輯:

我想這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); 
} 

欣賞的幫助。

感謝

回答

2

你有2-3選擇這樣做。我會告訴你這個選項對我來說是完美的。

只是UIScrollView類的子類,本地化您的UIImageView對象(意味着將其聲明在for循環本身。),將標籤分配給每個imageview。使UInmageView啓用userInteraction,否則它不會檢測到觸摸,然後在當前類中的UIScrollView子類&中編寫touchesBegan方法。然後編寫代碼處理GestureRecognizer。然後從UIScrollView的touchesBegan方法調用當前類的touchesBegan。

嘗試自己,學習一下自己。