通过使用 CGPoint 位置,它始终保存 uiscrollview 中的最后一个图像。当我点击其他图像进行保存时。我该怎么做才能保存我点击的确切图像。
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;
NSString *imageName = [NSString stringWithFormat:@"image%d.png", i];
_image = [UIImage imageNamed:imageName];
_imageView = [[UIImageView alloc] initWithImage:_image];
_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.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
- (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 savePhoto];
break;
default:
break;
}
-(void)savePhoto{
CGPoint location = [gesture locationInView:_imageView];
if (CGRectContainsPoint(_imageView.bounds, location)){
UIImageWriteToSavedPhotosAlbum(_image, self, @selector(image: didFinishSavingWithError:contextInfo:), nil);
}}}
任何想法将不胜感激。
Thanks
该点将始终出现在范围内UIScrollView
其中LongPressGestureRecognizer
被触发。你应该检查你的滚动视图contentOffset
(use contentOffset.x
对于水平布局和contentOffset.y
对于垂直布局)来检测您应该保存哪个图像。
此外,您还可以将触摸点转换为UIImageView
实例的局部坐标系并查看该点是否位于图像视图的范围内bounds
rect.
UPDATE
例如,您可以使用类似的方法来检测该点是否在图像视图的边界内(注意:我有not测试了这一点,这是假设有多个图像视图添加到滚动视图中):
if (CGRectContainsPoint(_imageView.bounds, [self.view convertPoint:location toView:_imageView]))
{
// do something
}
您还应该考虑检测之前应保存哪个图像,并在显示之前存储对该图像的引用UIActionSheet
对用户来说,因为它可能会减少您可能遇到的潜在问题的数量,并且以后更容易阅读,但这是我的主观意见。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)