1

我有添加了子视图(图片)的 UIScrollView。每次用户触摸滚动视图中的图片时,它都会在其顶部切换一个复选标记。

NSMutableIndexSet *picturesArray; <- 在 .h 中声明

- (void) touchesEnded: (NSSet *) touches withEvent: (UIEvent *) event {
if (!self.dragging) {
    [self.nextResponder touchesEnded: touches withEvent:event]; 
    NSLog(@"Touch down");
    for (UITouch *touch in touches) {

        for (int i = 1; i <= [self subviews].count; i++)
        {


        if(CGRectContainsPoint([[self viewWithTag:i]frame], [touch locationInView:self])){      
            NSLog(@"touched %d th view",i);

            NSArray *subviews = [[self viewWithTag:i] subviews];
            UIImageView *view = nil;

            view = [subviews objectAtIndex:0];

                if(view.hidden){
                    // add the index
                    [picturesArray addIndex:i]; 
                    view.hidden = NO; //check mark is shown
                }else{
                    [picturesArray removeIndex:i]; 
                    view.hidden = YES; //check mark is not shown
                }



            //  UIImageWriteToSavedPhotosAlbum([(UIImageView *)[self viewWithTag:i]image], nil, nil, nil); <- WORKS IF CALLED

            }

        }   
    }

}   

问题1:这是最好的方法吗?似乎使用 for (int i = 1; i <= [self subviews].count; i++) 很慢。我基本上需要捕捉被触摸的子视图。除了通过每个子视图之外,我还没有弄清楚这一点

调用 savePhotos 并基本上搜索触摸了哪些图片并将它们保存到相册。但是,对 UIImageWriteToSavedPhotosAlbum 的调用失败。这与 TouchesEnded 在同一个文件中。但是当在 TouchesEnded 中调用时,它可以工作。

  • (IBAction) savePhotos: (id) sender{

    NSLog(@"索引集为 %@",picturesArray );

    const NSUInteger arrayCount = picturesArray.count;

    NSUInteger *theIndexBuffer = (NSUInteger *)calloc(picturesArray.count, sizeof(NSUInteger)); UIImageWriteToSavedPhotosAlbum([(UIImageView *)[self viewWithTag:0]image], nil, nil, nil);

    [picturesArray getIndexes:theIndexBuffer maxCount:arrayCount inIndexRange:nil];

    for(int i = 0; i < arrayCount; i ++){

    NSLog(@"Element is %d",theIndexBuffer[i]);      
       UIImageWriteToSavedPhotosAlbum([(UIImageView *)[self viewWithTag:i]image], nil, nil, nil); <- THIS CRASHES 
    

    }

}

问题 2:为什么 UIImageWriteToSavedPhotosAlbum 失败了?

4

2 回答 2

2

1) 不使用 UIImageView,而是实现 UIImageView 的子项。然后尝试聆听对各个子视图的触摸,这应该可以解决您的 O(n) 问题

2)某些东西可能会自动释放,请仔细检查您的引用计数是否正确

于 2009-04-30T20:02:27.280 回答
0
  1. 尝试 UIView* targetView = [self hitTest:location withEvent:nil];
于 2009-04-30T21:15:46.090 回答