2012-10-09 15 views
0

目前,它保存的所有圖像,但我想只保存其中長按一個不是一次性全部圖像只有一個用戶選擇如何保存僅長按和由用戶選擇的圖像

- (void)viewDidLoad 

{ self.view.backgroundColor = [UIColor blackColor]; 
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; 
    UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

    [myButton addTarget:self action:@selector(dismissView:) forControlEvents:UIControlEventTouchUpInside]; 

    myButton.frame = CGRectMake(xOrigin, 10, 60, 35); 

    //[myButton addGestureRecognizer:tap]; 

    [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]; 
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
    _imageView.tag = 128; 
    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 addGestureRecognizer:gestureRecognizer]; 
    gestureRecognizer.delegate = self; 
    [gestureRecognizer release]; 

    [imageScrollView addSubview:imageView]; 
    [imageScrollView addSubview:myButton]; 

    //[imageView release]; 

} 
imageScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height); 

[self.view addSubview:imageScrollView]; 
[imageScrollView release]; 
} 


- (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 { 
     NSInteger numberOfViews = 61; 
     for (int i = 0; i < numberOfViews; i++) { 
      NSString *imageName = [NSString stringWithFormat:@"image%d.png", i]; 
      UIImage *image = [UIImage imageNamed:imageName]; 
      UIImageView *imageView = [[UIImageView alloc] initWithImage:image]; 
     UIImageWriteToSavedPhotosAlbum(imageView.image, self, @selector(image: didFinishSavingWithError:contextInfo:), nil); 


} 
} 

如何爲

感謝代碼幫助

回答

1

handleLongPress:功能,使用此:

UIGestureRecognizer *gestureRecognizer; 
CGPoint gesturePoint = [gestureRecognizer locationInView:self.view]; 

然後使用for循環檢查所有按鈕,就像您在那裏一樣。用它來檢查它是否是按下的圖像:

if(CGRectContainsPoint(myButton.frame,gesturePoint)){ 
    //save the image stored in myButton 

    //break will exit the for loop early, so if the view comes early in the list, 
    //it doesn't have to check the rest. 
    break; 
} 
相關問題