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


}
}

如何为此编码

感谢帮助

4

1 回答 1

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;
}
于 2012-10-10T03:57:44.813 回答