title: Captured which subview is touched in UiScrollView but cannot access it in another function.
body: <p>I have UIScrollView which has subviews (pictures) added do it. each time a user touches the a picture in the scrollview, it toggles a checkmark ontop of it.</p>
  
  <p>NSMutableIndexSet *picturesArray; &lt;- declared in .h</p>
  
  <pre><code>- (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 &lt;= [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); &lt;- WORKS IF CALLED
  
              }
  
          }   
      }
  
  }   
  </code></pre>
  
  <p>Question 1: is this the best way of doing this? It seems like  using a for (int i = 1; i &lt;= [self subviews].count; i++) is pretty slow. I basically need to capture which subview was touched. I havent figured this out other than going through EACH subview</p>
  
  <p>savePhotos is called and basically searches through which of the pictures was touched and saves them to the Photo Album. However the call to UIImageWriteToSavedPhotosAlbum fails. This is in the same file as TouchesEnded. But when called in TouchesEnded, it works.</p>
  
  <ul>
  <li><p>(IBAction) savePhotos: (id) sender{</p>
  
  <p>NSLog(@"The index set is %@",picturesArray );</p>
  
  <p>const NSUInteger arrayCount = picturesArray.count;</p>
  
  <p>NSUInteger *theIndexBuffer = (NSUInteger *)calloc(picturesArray.count, sizeof(NSUInteger));
  UIImageWriteToSavedPhotosAlbum([(UIImageView *)[self viewWithTag:0]image], nil, nil, nil);</p>
  
  <p>[picturesArray getIndexes:theIndexBuffer maxCount:arrayCount inIndexRange:nil];</p>
  
  <p>for(int i = 0; i &lt; arrayCount; i ++){</p>
  
  <pre><code>NSLog(@"Element is %d",theIndexBuffer[i]);      
     UIImageWriteToSavedPhotosAlbum([(UIImageView *)[self viewWithTag:i]image], nil, nil, nil); &lt;- THIS CRASHES 
  </code></pre>
  
  <p>}</p></li>
  </ul>
  
  <p>}</p>
  
  <p>Question 2: Why is it that UIImageWriteToSavedPhotosAlbum is failing ?</p>